توجه: سوالها لزوما به ترتیب سختی مرتب نشدهاند و ترتیب آنها (تقریبا!) اتفاقی است. استفاده از منابع از پیش آماده شده و دیکشنریها در حین مسابقه مجاز است.
برای سرعت بیشتر برنامهها، بجای پایتون میتوانید با PyPy کدتان را ارسال کنید.
لینکهای مفید برای شرکت در مسابقه:
میتوانید سوالهای خود را از بخش "سوال بپرسید" مطرح کنید.
In a little town called Jabolgha, people are living. Each of them has borrowed some money from exactly one other inhabitant of Jabolgha. Now the time has come to pay back all the debts, but the only problem is that everybody has spent all of their money!
The mayor of Jabolgha has decided to solve this problem. The town will give money to a few people (by inviting them to a variant of Squid Game where they can all win some money) so that they can pay back their debts. When some people get their money back, a chain reaction is started - for example, person A gets money from the game. Person A uses that money to pay their debt toward person B. Person B then uses that money to pay the debt towards person C, etc. If person B didn’t have enough money to pay back the debt, they wait until they get enough. If they have more than enough money, person B will keep what is left after payback.
Another example: if two people live in Jabolgha, and they owe Chugh (Chugh is the currency of Jabolgha) to each other, the town will give Chugh to one of them so they can pay back the debt to the other one.
Note that people may earn different amounts of money from the game.
Your task is to calculate the minimum total amount of money the game has to give to some subset of the inhabitants so that after the payback protocol described above all debts are paid.
The first line of input contains one integer , the number of inhabitants of Jabolgha. They are numbered from to .
The following lines contain two integers, separated by space. In -th of those lines, the first number - represents the id of the person -th person owes money to, and second represents the amount of the debt in Chugh.
Print a single integer - the minimum total amount of money the game in Jabolgha has to give to its inhabitant players so all debts are returned.