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://fp.icontool.com/go.php?url=https://onlinecasino.us.org/  http://www.battlegreenresearch.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://healthclick.tc/__media__/js/trademark.php?d=onlinecasino.us.org   http://www.stclairbaptist.org/System/UserProfile.asp?id=44602&func=signup&referer=https://onlinecasino.us.org  http://fa.arannews.com/?pageid=1&MID=22&url=https://onlinecasino.us.org   http://dermalight.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.poetrytogo.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://drdelange.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.narrateconferences.org/go.php?redir=https://onlinecasino.us.org 
http://therapyswings.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://bonuskins.com/kak-skraftit-ak-47-krasnaya-liniya-v-ks-go/https://onlinecasino.us.org   http://investinbaltimore.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://creditunionlaw.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://longtermculture.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://troyanassociates.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://ruralhealthpilot.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.divhost.ru/go.php?go=https://onlinecasino.us.org   http://dreamit-buildit.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://autoconfig.ru/redirect.php?url=onlinecasino.us.org  http://www.rhythmking.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://lexiconbranding.biz/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.forsimpler.com/comment/comment?newsid=3129786&encoding=UTF-8&data=AC_BugAAABUAAC__AAAAAQBL44CK5pWw5a2X5raI6LS55Ye654mI6aKE5rWL44CL5oql5ZGK77ya5YWo55CD57q45aqS5Y-R5bGV6IO95Yqb5oiW6KKr5L2O5LywAAAAAAAAAAAAAAAvMC0CFF3KYM68TrEtSMNjD2npyA8wZnX7AhUAhbhWbrTnVA-XsqElaHgqPCNavAs.&referer=https://onlinecasino.us.org  http://amedicinalmind.libsyn.com/website?referer=https://onlinecasino.us.org   http://edgetoronto.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://westofchicagonow.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://pyonta.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://sphreg.asiaone.com/RegAuth2/stsSignUp.html?goto=https://onlinecasino.us.org 
http://server.dorama-fan.ru/go/?https://onlinecasino.us.org  http://sicari.us/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://cabinetarchitect.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.tactilewood.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.impaacthiv.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://ccmargarita.com/phpinfo.php?a<>=<a+href=https://onlinecasino.us.org  http://www.blitz-performance.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://pxd.cvstarr.tw/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.freeadultwebcams.org/cgi-bin/at3/out.cgi?s=70&l=thumb&c=1&u=https://onlinecasino.us.org 
http://hearing-aids-ohio.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://tranchecellars.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://continuumtochrist.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://indiagoldpolicycentre.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://boobsandboospodcast.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.resl-trk.com/redirect.php?client=1363595270-27428&url=https://onlinecasino.us.org  http://www.toponlinedegree.sale/cgi-bin/atx/out.cgi?c=0&l=gal&u=/onlinecasino.us.org  http://gasolinemoney.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://gbcode.ofca.gov.hk/TuniS/onlinecasino.us.org 
http://bankmortgageservices.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://uc.sjedu.cn/uchome/link.php?url=https://onlinecasino.us.org   http://kredit-pod-zalog.narod.ru/go?https://onlinecasino.us.org   http://carmelinsider.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://asianteentube.net/cgi-bin/crtr/out.cgi?url=https://onlinecasino.us.org/   http://tripwow.ru/go.php?url=https://onlinecasino.us.org  http://epnet.info/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://parentnetwork.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://nextraqstore.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://mingle.im/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://touchcheats.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://76.ru/feedback/mailto.php?m=li%2B2E9sMVU%2BSnv8MahWG5A%3D%3D&u=0QqRZytY0z8USbvoif9qDq6kjTMtd3pTm%2FcjdbDrzj1uW7zpwTlwqrOVKTNlGHGZkxkTHU%2BkxDM%3D&referer=https://onlinecasino.us.org   http://www.junic.ca/en/envoiami/index.php?url=https://onlinecasino.us.org  http://preferredvoices.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://bbs.qidian.ca/redirect.php?tid=23938&goto=onlinecasino.us.org  http://ecastcovers.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://maryfrieloconnor.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://mmjames.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://freepressphotos.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://kvu.su/bitrix/rk.php?id=276&site_id=s1&event1=banner&event2=click&event3=17+%2F+%5B276%5D+%5BA1%5D+%D0%A1%D1%82%D0%B0%D0%B6%D0%B5%D1%80&goto=https://onlinecasino.us.org   http://www.wzdq123.com/go.php?url=https://onlinecasino.us.org   http://kosairchildrenscharity.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://axoft.ru/bitrix/redirect.php?event1=&event2=&event3=&goto=https://onlinecasino.us.org   http://jackteklimited.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://nationstarsloan.info/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.5minute.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://postfach.biz/exchweb/bin/redir.asp?url=https://onlinecasino.us.org 
http://ygd.animelyric.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://razdelbittexno.biz-market.ru/jump.php?url=https://onlinecasino.us.org   http://intellomotive.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.schanghai.info/?p=&sl=cn&cjjobid=16354&partner=onlinecasino.us.org  http://nancyamvaughn.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://terikehkeloonga.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.jewishmonumentforless.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.ravendev.lt/DasBlog/ct.ashx?id=c89e2c07-f2f7-4e21-9507-4d044311c77d&url=https://onlinecasino.us.org   http://www.travels-guide.net/ref_gamer_com/redir.php?url=https://onlinecasino.us.org 
http://lbsreview.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://lasvegasmagazine.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://virtualmanaging.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://greeningarkansas.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.highrisestyle.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://girlsofthepacten.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://cluttertherapy.us/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://insurancemgas.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://foodtoyou.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
 
 
 
 
 
http://anasozu.com/bu-bokluklardan-tarafimizi-kim-paklayacek/#comment-9291
http://forum.sonturkrpg.org/showthread.php?tid=99599&pid=157341#pid157341
https://lavoroabergamo.it/index.php?topic=664397.new#new
https://forum.itabcabc.ru/viewtopic.php?f=5&t=233935
http://sifasudan.org/forum/showthread.php?tid=25073&pid=52541#pid52541