Problem:
Score : 1400 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

In the country there are N cities numbered 1 through N, which are connected by N-1 bidirectional roads. In terms of graph theory, there is a unique simple path connecting every pair of cities. That is, the N cities form a tree. Besides, if we view city 1 as the root of this tree, the tree will be a full binary tree (a tree is a full binary tree if and only if every non-leaf vertex in the tree has exactly two children). Roads are numbered 1 through N-1. The i-th road connects city i+1 and city a_i. When you pass through road i, the toll you should pay is v_i (if v_i is 0, it indicates the road does not require a toll).

A company in city 1 will give each employee a family travel and cover a large part of the tolls incurred. Each employee can design the travel by themselves. That is, which cities he/she wants to visit in each day and which city to stay at each night. However, there are some constraints. More details are as follows:

The travel must start and end at city 1. If there are m leaves in the tree formed by the cities, then the number of days in the travel must be m+1. At the end of each day, except for the last day, the employee must stay at some hotel in a leaf city. During the whole travel, the employee must stay at all leaf cities exactly once.

During the whole travel, all roads of the country must be passed through exactly twice.

The amount that the employee must pay for tolls by him/herself is the maximum total toll incurred in a single day during the travel, except the first day and the last day. The remaining tolls will be covered by the company.

Shik, as an employee of this company, has only one hope for his travel. He hopes that the amount he must pay for tolls by himself will be as small as possible. Please help Shik to design the travel which satisfies his hope.

Constraints

2 < N < 131,072

1 \leq a_i \leq i for all i

0 \leq v_i \leq 131,072

v_i is an integer

The given tree is a full binary tree

Input

The input is given from Standard Input in the following format:

N
a_1 v_1
a_2 v_2
:
a_{N-1} v_{N-1}

Output

Print an integer denoting the minimum amount Shik must pay by himself for tolls incurred during the travel.

Sample Input 1

7
1 1
1 1
2 1
2 1
3 1
3 1

Sample Output 1

4

There are 4 leaves in the tree formed by the cities (cities 4, 5, 6, and 7), so the travel must be 5 days long. There are 4! = 24 possible arrangements of the leaf cities to stay during the travel, but some of them are invalid. For example, (4,5,6,7) and (6,7,5,4) are valid orders, but (5,6,7,4) is invalid because the route passes 4 times through the road connecting city 1 and city 2. The figure below shows the routes of the travel for these arrangements.

For all valid orders, day 3 is the day with the maximum total incurred toll of 4, passing through 4 roads.

Sample Input 2

9
1 2
1 2
3 2
3 2
5 2
5 2
7 2
7 2

Sample Output 2

6

The following figure shows the route of the travel for one possible arrangement of the stayed cities for the solution. Note that the tolls incurred in the first day and the last day are always covered by the company.

Sample Input 3

15
1 2
1 5
3 3
4 3
4 3
6 5
5 4
5 3
6 3
3 2
11 5
11 3
13 2
13 1

Sample Output 3

15

Sample Input 4

3
1 0
1 0

Sample Output 4

0

Solution in Common Lisp (SBCL 1.1.14) (judge verdict: Runtime Error)

http://www.x3m-master.ru/forum1/member.php?action=profile&uid=50894  http://188login.com/data/profile.php?id=154190   http://www.musclecoalition.org/member.php?11430-Wizzabqcet   http://favcrav.com/users/dimmjupaund  http://satillimite.com/profile.php?id=29890   http://forum.forexmon.ru/member.php/1325-DimmswAmeda  https://testoriginal.com/forum/member.php?action=profile&uid=49  http://www.association-manoirducrime.fr/forum/profile.php?id=145875   http://forum.fantasylibrary.ir/member.php?554-WizzrkCak 
http://nedeli.org/mama/user/52078/  http://sbo-bet.online/member.php?action=profile&uid=18019   http://supramannen.com/member.php?u=889830   http://surja.waw.pl/forum/member.php?action=profile&uid=5146  https://beta.i4g.tech/member.php?action=profile&uid=1072   http://g-ghoneim.com/vb/member.php?u=201133  https://ravedmc.com/forum/member.php?action=profile&uid=32  http://www.astronomyforum.net/members/wizzdnerorp.html   http://taaleb.ir/member.php?259-WizzaytVon 
https://www.mydeepfakes.com/community/member.php?action=profile&uid=3212  http://forum.kobietazklasa.pl/index.php?action=profile;u=62552   http://www.forumprint.ru/user/Diimmioralk/   http://www.astronomyhubble.com/member.php?action=profile&uid=3000  https://flat8.life/member.php?action=profile&uid=279   http://www.salammasjid.com/forum/member.php?action=profile&uid=292  https://forum.awarz.net/member.php?157594-WizzaryTusly  http://simabar.golestanmporg.ir/user/DimmyqGes/   http://forum.azescience.org/member.php?action=profile&uid=136 
http://www.dipkursak.ru/user/Dimmhhskymn/  http://waslala.net/user/13705   http://www.iigenius.cn/memberlist.php?mode=viewprofile&u=59552   http://www.fawnisland.com/forum/index.php?action=profile;u=25248  http://forums.zenbership.com/profile.php?id=25968   https://www.crypto-oc.com/vb/member.php?u=207  http://community.thcfinder.com/member.php/134125-Dizzajinicy  http://planetasofta.org/member.php?u=24420   http://hexsurvival.ga/member.php?action=profile&uid=5274 
http://bhcsaint.org/forum/member.php?action=profile&uid=407  http://fabiwan.kenobi.free.fr/forum/profile.php?id=3233   http://gilservis.maglan.ru/user/Wizzaxnpat/   http://lordmike.eu/forum/member.php?u=48470  https://alumni.akademitelkom.ac.id/member.php?action=profile&uid=62552   https://goddl.net/forum/member.php?u=46  http://pinjraa.com/forum/member.php?action=profile&uid=160  http://www.animschoolforums.com/memberlist.php?mode=viewprofile&u=49701   http://supramannen.com/member.php?u=889784 
https://beta.i4g.tech/member.php?action=profile&uid=1074  http://forum.onlinefootballmanager.ru/members/947-Dizztdlom   http://sch112.ru/user/Dimmjjglymn/   http://www.cactuscultivation.com/forum/memberlist.php?mode=viewprofile&u=2350  http://forum.granit-micro.ru/memberlist.php?mode=viewprofile&u=25831   http://olga-bud.com.ua/user/Dizzverow/  http://www.musclecoalition.org/member.php?11433-Aiddsmcet  http://s-uohb.com/member.php?101545-AiidsyvaR   http://sr.radixedu.org.ve/member.php?action=profile&uid=2515 
http://coarte.com.ec/index.php?option=com_k2&view=itemlist&task=user&id=35538  http://www.elitesquadgaming.com/forum/member.php?action=profile&uid=429   http://www.vfxtalk.org/member.php?63063-WizzaxaRerne   http://unlimitedgamingonline.com/member.php?action=profile&uid=339  http://w88w88top.com/w88top/profile.php?id=57728   http://www.802poker.com/member.php?action=profile&uid=63660  http://devops-india.com/profile.php?id=117670  https://anunciogratis.com.br/author/aiidfzstoff/   https://testoriginal.com/forum/member.php?action=profile&uid=51 
http://in-technologies.ru/forum/index.php?action=profile;u=218844  http://freelancer.site.ge/user/DizzxhObelo/   http://himeuta.org/member.php?224855-SoogkaUnobe   http://pioneeri.ru/forum/memberlist.php?mode=viewprofile&u=84471  http://olga-bud.com.ua/user/Wizzajtrow/   http://beau-tec.com/forum/member.php?action=profile&uid=1117  http://smulti.org/forum/member.php?action=profile&uid=496  http://perekhid.te.ua/user/WizzbiPaw/   http://jpacschoolclubs.co.uk/component/users/?option=com_k2&view=itemlist&task=user&id=2214343 
https://forum.antoniuscraft.nl/member.php?action=profile&uid=377  https://elizarovo.org/memberlist.php?mode=viewprofile&u=5297   https://testoriginal.com/forum/member.php?action=profile&uid=40   https://libriusatiunipv.altervista.org/member.php?action=profile&uid=122  https://ghostsclan.zapto.org/punbb/profile.php?id=13612   http://bbs.long-d.cn/users/62694/  https://forum.esforces.com/members/44991-Wizzapqric  http://adslife.ru/member.php?u=2214   http://computernetworktutor.com/fluxbb/profile.php?id=374298 
http://atm1forum.com/forum/member.php?u=4566  http://www.elitesquadgaming.com/forum/member.php?action=profile&uid=435   http://forum.onlinefootballmanager.fr/member.php?1960366-DizzjfTaw   https://www.adahor.com/author/diggnvpelia/  http://sr.radixedu.org.ve/member.php?action=profile&uid=2514   http://www.italicar.ru/forum/profile.php?id=887234  https://forum.msk24city.ru/memberlist.php?mode=viewprofile&u=15879  http://harwooj.co.uk/Epsommm/member.php?action=profile&uid=4480   http://kuohsar-salmas.ir/index.php?subaction=userinfo&user=DiggtiWhody 
 
 
 
 
 
https://digitallifegaming.com/forums/showthread.php?13610-Examples-of-appeals-letters-financial-aid&p=19589#post19589
https://www.lecoindealex.fr/Forum/viewtopic.php?f=4&t=32303
https://uosymphony.com/resources/intonation-solutions-for-orchestral-players/
http://dankonoy.com/forum/index.php/topic,59325.new.html#new
http://foro.salaolimpo.com/viewtopic.php?f=9&t=6273