- محدودیت زمان: ۲ ثانیه
- محدودیت حافظه: ۲۵۶ مگابایت
In a little town called Jabolgha, $N$ 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 $200$ Chugh (Chugh is the currency of Jabolgha) to each other, the town will give $200$ 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 $N$, the number of inhabitants of Jabolgha. They are numbered from $1$ to $N$.
The following $N$ lines contain two integers, separated by space. In $i$-th of those lines, the first number - $A_i$ represents the id of the person $i$-th person owes money to, and second $B_i$ represents the amount of the debt in Chugh.
$$2 \le N \le 200\ 000$$ $$1 \le A_i \le N, A_i \neq i$$ $$1 \le B_i \le 10\ 000$$
خروجی
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.
مثال
ورودی نمونه ۱
4
2 50
1 50
4 30
3 30
خروجی نمونه ۱
80
ورودی نمونه ۲
3
2 120
3 40
2 80
خروجی نمونه ۲
160
ورودی نمونه ۳
5
3 30
3 20
4 90
5 40
3 60
خروجی نمونه ۳
110
ارسال پاسخ برای این سؤال