本站作为LDUOnlineJudge的测试版本进行演示,官方网站请访问http://icpc.ldu.edu.cn
[Hidden]

1141. Dance


Description

ALO is a world of magic and elves. The magic can be upgraded and make more power. The elf Leafa occasionally gets a book that records the most powerful magic matrix. Now the wind elves have decided to conjure up the magic matrix, bombing out the most beautiful fireworks to celebrate their spirit festival.


The magic matrix is a combination of magic, which has a strict hierarchy (like a tree). The wind elves can produce the lowest level of magic directly. Low level magic can be upgraded to a higher magic by consuming one specified hand scroll, which will create magic power in the air and become a higher level of magic. 


The upgraded magic can still be upgraded to a higher level of magic until it reaches the highest level of magic. Once the magic reach the highest level, the magic matrix will absorb all the magic power in the air. Note magic upgradation is a continuous process, which means you must perform a series of upgradation at a time, in order to make a lowest level of magic into the highest. If you don’t continuously upgraded to the highest level, the magic you made and the magic power the process produced in the air will disappear immediately. 


Now the wind elves took all the magic hands scroll out and began to read the magic books that Leafa had found. As the wisest wind elves, the Lord wants you to help build the magic matrix with the most magic power.

Input

The first line includes a integer number n(n ≤ 100000), types of magic (not including the highest magic, and the highest magic is magic 0)
Then there are n line. Each line includes three numbers, and in the i-th line: 
The first integer is prei(0 ≤ prei ≤ n) , that the magic i can be upgraded to the magic prei.
The second integer is numi(numi ≤ 40000), which means wind elves have numi hand scroll to perform such upgradation.
The third integer is poweri(poweri ≤ 1000), which means one such upgradation will produce poweri magic power in the air.

Output

One number, the most magic power.

Samples

Input Copy
7
0 100 0
1 2 3
2 2 5
1 5 1
2 1 3
3 2 4
4 3 2
Output
33

Source

第九届山东省赛
Problem Information

Time Limit: 1000MS (C/C++,Others×2)
Memory Limit: 128MB (C/C++,Others×2)
Special Judge: No
AC/Submit: 1 / 1
Tags:
Contests involved

1017. 2018 SD ACM
My history solutions

You didn't submit any solution!
Submit your solution

Login Register