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)

https://p2pfrank.com/member.php?action=profile&uid=509  http://www.itlstexas.com/mybb/member.php?action=profile&uid=1712   http://www.uristconsult.com/forum/profile.php?id=7599   http://beautifultattoo.info/profile.php?uid=3194  http://pakcarclub.com/member.php?9090-WizzgtPal   https://www.projectw.org/member.php?320-AidddzLeasp  http://yourm88.com/m88bet/profile.php?id=27608  https://sat-sharing-info.tk/member.php?u=909   http://forum.papeterra.pl/member.php?action=profile&uid=765 
http://congnghexe.net/auto/profile.php?id=62791  http://www.eng.azrip.net/user/WizzaeeGlavy/   http://www.tnu.in.ua/forum/member.php?u=25938   http://dantespit.com/memberlist.php?mode=viewprofile&u=77  http://centralniy.org/memberlist.php?mode=viewprofile&u=5542   http://supramannen.com/member.php?u=889795  https://coachandbusmarket.com/author/wizzyuvam/  http://www.miniaturecattle.net/forum/member.php?action=profile&uid=117   http://animesay.ru/users/DizzviacEnd 
http://sxt.ch/forum/profile.php?id=25381  https://thailandelite.org/forum/User-DiggllAlami   http://gyandaata.com/profile/wizzrwkib   http://vinochok-dnz17.in.ua/user/Dizztitrone/  http://prochat.alpha-electronics.net/member.php/422613-DiimsvIrome   https://www.dangeroussports.com/forum/profile.php?id=27487  http://www.vegporn.com/board/member.php?u=351679  http://legalize.co/author/wizzanicen/   http://oolgaming.com/forum/member.php?action=profile&uid=5713 
http://oshobr.grodno.by/user/WizzasyNib/  http://simabar.golestanmporg.ir/user/DizzokGes/   https://forum.zenstudios.com/member.php?2508374-Garrrygwtaw   http://dmvyouth.soccer/memberlist.php?mode=viewprofile&u=171  http://gamesvuinhon.com/vuinhon/profile.php?id=52280   http://kuohsar-salmas.ir/index.php?subaction=userinfo&user=DiggxmWhody  http://htlsr.de/forum/member.php?action=profile&uid=55080  http://conceptsnclarity.com/forum/index.php?action=profile;u=9226   http://forum.knightsofcamelot.info/member.php?action=profile&uid=2208 
http://bio.mdu.edu.ua/index.php?subaction=userinfo&user=DizzzjOpina  http://www.v-tadawul.net/forum/member.php?1179447-Dizzikjed   http://www.opencart.com.tr/member.php?action=profile&uid=16350   http://bongdapluz.com/bongdaso/profile.php?id=29179  https://kuliah.trisnowlaharwetan.net/user-4885.html   https://xossipp.com/member.php?action=profile&uid=41  http://theposhmedia.com/forum/member.php?action=profile&uid=114  http://newdigital-world.com/members/dizzynurgen.html   https://excellence.org.il/space/forum/profile.php?id=87240 
http://victorscu.com.es/mybb/member.php?action=profile&uid=10  http://byw.su/member.php?action=profile&uid=6888   http://girlmachine.com/forum/profile.php?id=6466   http://probeersel.com/forum/profile.php?id=63484  http://favcrav.com/users/dimmufpaund   http://byw.su/member.php?action=profile&uid=6860  http://arabforums.net/member.php?action=profile&uid=82  http://www.dzzzr.org.ua/forum/member.php?u=142411   http://www.capitolasset.net/forum/member.php?action=profile&uid=948 
https://vw88vn.com/forum/profile.php?id=191977  http://forum.fantasylibrary.ir/member.php?553-WizzpcCak   http://forum.azescience.org/member.php?action=profile&uid=331   http://www.civfanatics.ru/members/77660-GarrrywlRazic  http://marjorieaperry.com/index.php/component/users/?option=com_k2&view=itemlist&task=user&id=372522   http://meyxana.net/forum/member.php?32541-AiddowCow  http://kqxs88.com/xoso/profile.php?id=8457  http://www.eng.azrip.net/user/DiggfsGlavy/   http://www.dangelodesigncenter.com/forum/index.php?action=profile;u=37136 
https://forum.zenstudios.com/member.php?2527428-Diggkokep  http://finansy.su/user/WizzhzStils/   http://www.fawnisland.com/forum/index.php?action=profile;u=25404   https://ant53.ru/user/49468/  https://sahiplenelim.com/forum/member.php?action=profile&uid=52   http://visiology.com/forum/user/550/  http://myfileshost.org/Forum/member.php?action=profile&uid=521  http://tnortho.org/forum/forums/users/wizzalhbunny/   http://filehot.net/user/WizzatlDrupt/ 
http://www.homemadetools.net/forum/members/digguxquise/  http://www.giveupalready.com/member.php?83079-Garrrynajet   https://www.advogadoonline.net/profile/wizzajkemoda   https://www.iroids.com/forums/member.php?29306-Wizzalwgloth  http://www.espresso-freunde.de/index.php?option=com_k2&view=itemlist&task=user&id=49434&Itemid=2653   http://www.forumsepakbola.com/member.php?29275-DizzrfLig  https://mavenhacks.net/member.php?action=profile&uid=51  http://school1925-rzn.ru/osnovnye-svedeniya/user/5814/   http://forumbalada.com/member.php?action=profile&uid=20275 
http://incatalogues.ru/bbs/tools.php?event=profile&pname=DizzpbdernE  http://bong88i.com/forum/profile.php?id=106553   http://www.espresso-freunde.de/index.php?option=com_k2&view=itemlist&task=user&id=49399&Itemid=2653   https://forum.esforces.com/members/45022-Dizzoxric  http://newdigital-world.com/members/aiidtxurgen.html   http://vincityleseine.com/vin/profile.php?id=80619  http://idina-here.com/gallery/profile.php?uid=34617  http://bhcsaint.org/forum/member.php?action=profile&uid=416   http://gallery.needforspeed.sk/profile.php?uid=510888 
 
 
 
 
 
http://w88vn.today/nap-tien-vao-nha-cai-w88-choi-ca-cuoc-bong-da/#comment-251
http://fabiwan.kenobi.free.fr/forum/viewtopic.php?pid=27654#p27654
http://htlsr.de/forum/showthread.php?tid=10213
http://ghanasdgs.info/2016/04/20/these-7-things-will-change-the-way-you-approach-travel/#comment-30938
http://www.vectorrepair.com/forum/index.php/topic,11778.new.html#new