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.palace3xxx.com/memberlist.php?mode=viewprofile&u=365  http://forum.caothumu.com/member.php?2561-Aiiddmsnamn   http://web.mdu.edu.ua/index.php?subaction=userinfo&user=AiidmzPep   http://forum.transqazvinajo.ir/member.php?8990-Dizznedus  http://huunhan.name.vn/member.php?4074-DizztdHax   http://vinochok-dnz17.in.ua/user/Dimmhptrone/  http://forum.mpc-scolaire.fr/profile.php?id=28053  http://www.specserv.us/forums/member.php?action=profile&uid=699   https://nattyornotforum.com/member.php?action=profile&uid=293 
http://teykovo-online.ru/users/Wizzgdatrow  http://katanasword.ru/user/Soogcvpet/   http://s-uohb.com/member.php?101385-WizzarqvaR   https://f.nedelia.lt/user/Aiidqgnon/  http://muboss.net/forum/member.php?action=profile&uid=48691   https://woahoverflow.org/forums/index.php?action=profile;u=32  http://pozyskajdotacje.pl/dizzhnorike-u23297.html  http://www.femdommedia.com/forum/member.php?action=profile&uid=471   http://zoovetservis.com/board/tools.php?event=profile&pname=WizzazbRen 
http://www.laboutiquedelautomovil.com/forum.laboutiquedelautomovil.com/member.php?action=profile&uid=93  http://smulti.org/forum/member.php?action=profile&uid=99   http://mundo.adjaranet.com/user/Garrryxesot/   https://artsolo.pl/forum/profile.php?id=9251  http://az-serwer1859764.online.pro/member.php?action=profile&uid=16537   http://www.shaleisland.com/member.php?90-DigylBak  https://crochettalk.com/members/dizztqrah/  http://archeslocal.org.uk/forum/member.php?action=profile&uid=126552   https://megnevezhetetlen.000webhostapp.com/member.php?action=profile&uid=8750 
http://tournamentsinpoker.com/member.php?action=profile&uid=56355  http://foreveralways.com.au/Gallery/profile.php?uid=580   http://tnortho.org/forum/forums/users/wizzauzbunny/   http://www.dostindian.com/author/dimmdxjak/  http://forums.sanityrp.us/member.php?action=profile&uid=47   http://k88pro.com/k8/profile.php?id=14967  http://www.castlecreator.com/forums/user/aiidgjlarce/  http://gamesvuinhon.com/vuinhon/profile.php?id=51685   http://forumjudi.co/profile.php?id=123803 
http://www.lbryk.pl/forum/member.php?action=profile&uid=6977  http://futsal5cotland.com/profile.php?id=9051   http://uselessrecords.com/forum/index.php?action=profile;u=221   https://diendanmuaban.xyz/member.php?211-dimmqbwib-html  http://www.forum.hertz-audio.com.ua/memberlist.php?mode=viewprofile&u=2727669   http://france-publication.com/author/dizzngament/  https://titanetch.com/forum/profile.php?id=7729  http://www.802poker.com/member.php?action=profile&uid=63653   http://chalk.education/users/details/d21f7e3c-7e83-4141-80f9-c0c284db4bd3 
http://forum.cntt2a2.com/user-20835.html  http://mt2shr8.com/vb/member.php?u=13802   http://81.222.238.42/member.php?10537-WizzantHeall   http://www.govblacklist.com/memberlist.php?mode=viewprofile&u=126  https://tminer.hbws.uk/member.php?action=profile&uid=40   http://www.technodom-blog.com/forums/profile.php?id=80345  http://karpich.com.ua/user/DizzlaPax/  http://g-drugz.fr/forum/member.php?action=profile&uid=543   http://www.akadeemiake.ee/foorum/profile.php?id=22924 
http://www.tvoyopravo.ru/users/WizzihCashy  http://www.heroturko.net/user/DizzrxBense/   http://www.atheist-poetry.com/forums/member.php?action=profile&uid=380   https://www.adahor.com/author/wizzgvpelia/  http://favcrav.com/users/wizzkdpaund   https://ogomago.ml/user-7236.html  http://macanqq.gq/member.php?action=profile&uid=12  http://nazaninmousavi.com/index.php?subaction=userinfo&user=DiggemTok   http://www.angelworker.com/forum/member.php?action=profile&uid=575 
http://forum.vikingstown.at/profile.php?id=15973  http://ant0n.net/forums/member.php?action=profile&uid=25   https://darkwarez.tv/member.php?3-Garrryohtap   http://tournamentsinpoker.com/member.php?action=profile&uid=56355  https://www.7sportnews.ru/user/WizzrdHek/   http://prorabforum.com/member.php?u=6369  http://skinion.net/member.php?66685-Wizzaeekem  https://www.dangeroussports.com/forum/profile.php?id=28456   https://elizarovo.org/memberlist.php?mode=viewprofile&u=5290 
http://www.strengthandflexibility.com/forum/profile.php?id=51763  http://karpich.com.ua/user/WizzatbPax/   http://satillimite.com/profile.php?id=29862   https://www.7autonews.ru/user/Dimmivkam/  http://xxxextreme.org/user/WizzaobEmesy/   http://www.itlstexas.com/mybb/member.php?action=profile&uid=1696  http://www.informaticos.pt/forum/member.php?action=profile&uid=242542  http://www.vfxtalk.org/member.php?63062-WizzagcRerne   http://motorsportracingcommunity.com/member.php?action=profile&uid=152 
https://ghostsclan.zapto.org/punbb/profile.php?id=14872  http://bong88i.com/forum/profile.php?id=128773   https://forum.bulletstormgaming.com/member.php?action=profile&uid=18   http://www.apnanri.com/forum/profile.php?id=20954  https://nyt.cloud/member.php?action=profile&uid=531   https://darkwarez.tv/member.php?22-Diggapbip  http://autoniha.com/member.php?action=profile&uid=134  http://blackhatstudent.com/member.php?action=profile&uid=860   http://player.ru/member.php?u=161997 
 
 
 
 
 
http://zhgtk.kz/forum/viewtopic.php?pid=470688#p470688
http://www.vektor.hr/forum/viewtopic.php?pid=31574#p31574
http://forum.2dar2.de/index.php?topic=126180.new#new
https://www.kadmium.nl/forum/viewtopic.php?f=3&t=10159
https://forum.extreme-server.com/index.php?topic=221856.new#new