توجه: سوال‌ها لزوما به ترتیب سختی مرتب نشده‌اند و ترتیب آن‌ها (تقریبا!) اتفاقی است. استفاده از منابع از پیش آماده شده و دیکشنری‌ها در حین مسابقه مجاز است.

برای سرعت بیشتر برنامه‌ها، بجای پایتون می‌توانید با PyPy کدتان را ارسال کنید.

لینک‌های مفید برای شرکت در مسابقه:

می‌توانید سوال‌های خود را از بخش "سوال بپرسید" مطرح کنید.

Squid Game (2021)


  • Memory Limit: 512MB
  • Time Limit: 2sec

In a little town called Jabolgha, NN 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 200200 Chugh (Chugh is the currency of Jabolgha) to each other, the town will give 200200 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.

Input🔗

The first line of input contains one integer NN, the number of inhabitants of Jabolgha. They are numbered from 11 to NN.

The following NN lines contain two integers, separated by space. In ii-th of those lines, the first number - AiA_i represents the id of the person ii-th person owes money to, and second BiB_i represents the amount of the debt in Chugh.

2N200 0002 \le N \le 200\ 000 1AiN,Aii1 \le A_i \le N, A_i \neq i 1Bi10 0001 \le B_i \le 10\ 000

Output🔗

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.

Sample Input 1🔗

4
2 50
1 50
4 30
3 30
Plain text

Sample Output 1🔗

80
Plain text

Sample Input 2🔗

3
2 120
3 40
2 80
Plain text

Sample Output 2🔗

160
Plain text

Sample Input 3🔗

5
3 30
3 20
4 90
5 40
3 60
Plain text

Sample Output 3🔗

110
Plain text
ارسال پاسخ برای این سؤال
در حال حاضر شما دسترسی ندارید.