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://virtualmortgagefunding.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.godspoketome.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.worldwidehelpers.org/index.php?url=https://onlinecasino888.us.org   http://www.annasfunhouse.com/cgi-bin/autorank/out.cgi?id=janetexp&url=https://onlinecasino888.us.org  http://hbn.lamritewest.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://flippenteenleadership.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://go-nsg.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://safety.tbzmed.ac.ir/?pageid=1&MID=22&url=https://onlinecasino888.us.org   http://intra.kmiasset.com/index.php?mid=www_banner&document_srl=25640&fon=FFFFFF&txt=76625B&stxt=76625B&page=6&fromurl=onlinecasino888.us.org 
http://picsndquotes.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://embracetheinternet.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://iqrsystems.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://borescope-inspection.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://saferwarninglabels.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://dogbreedsbysize.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://oresama.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.dpgberlin.de/de/service/links/?go=https://onlinecasino888.us.org   http://internaldisplacement.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://rolored.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.usacademics-it.com/redirect.php?school=StudyWisconsin&schoolid=na&language=it&adsize=200x100&adtype=gif&userip=66.249.66.223&redirect=https://onlinecasino888.us.org   http://www.iberosur.be/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.breastgirls.com/cgi-bin/autorank/out.cgi?id=iloveboo&url=https://onlinecasino888.us.org  http://www.wzdq.cc/go.php?url=https://onlinecasino888.us.org   http://www.usg.ru/redir.php?url=https://onlinecasino888.us.org  http://gadyach.pl.ua/go/url=onlinecasino888.us.org  http://pens-america.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://itland.ru/bitrix/rk.php?id=60&event1=banner&event2=click&event3=1+%2F+%5B60%5D+%5BRIGHT1%5D+%D2%E5%F5%ED%EE%EC%E8%F0&goto=onlinecasino888.us.org 
http://oiltanktest.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://rh.consorzioiris.net/?a<>=Coach emagrecimento sp (<a href=https://onlinecasino888.us.org   http://speedrewards.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://optimalquest.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://companystat.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://cobaltinternationalenergy.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://amazon.de.xx3.kz/go.php?url=https://onlinecasino888.us.org  http://ts-sex.com/cgi-bin/crtr/out.cgi?url=https://onlinecasino888.us.org   http://3danimeworld.com/cgi-bin/atx/out.cgi?id=329&trade=https://onlinecasino888.us.org 
http://qrk.elecpro.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://idahodoctorshospital.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://chemistry.xtu.xk.hnlat.com/index.php?m=currency&c=free&a=show&catid=2513&id=102&url=https://onlinecasino888.us.org   http://www.orizonti.com/LinkClick.aspx?link=https://onlinecasino888.us.org  http://ptn.longevityrich.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://svt.disasterreliefcamps.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://zhongyanfeng.com/wp-content/themes/begin/inc/go.php?url=https://onlinecasino888.us.org  http://baoos.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://608doctors.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://myhealtheview.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://samaritanspursetv.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://oaksuperstore.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.ebrands.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.itwasonline.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://dxlive.lead-channel.com/link.php?url=https://onlinecasino888.us.org  http://fdca71.e-monsite.com/liens/do/redirect/?url=https://onlinecasino888.us.org  http://www.anetowines.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.hotelicom.com/start/goto.php?cid=0&url=https://onlinecasino888.us.org 
http://plannedparenthoodvotesrhodeislandpac.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://mobile.systems/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://thomasvarsavsky.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://xju.4hopefoundation.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://whitandalexbedding.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.al-bis.ru/bitrix/redirect.php?event1=&event2=&event3=&goto=https://onlinecasino888.us.org  http://www.careerskillsfoundation.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.hillcountry247.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.italiansonline.net/clic.php?url=https://onlinecasino888.us.org/ 
http://overturecenterfoundation.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://tilghmanmichaels.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://traveldemand.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.s-r-s.jp/hinoki/cgi/mt_keitai/mt4i.cgi?id=3&mode=redirect&no=5&ref_eid=47&url=https://onlinecasino888.us.org  http://select-controls.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://sellyourhuntsvillehome.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.dvdjazz.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.loveshack.org/a/delivery/lg.php?bannerid=80&amp;campaignid=45&amp;zoneid=67&amp;source=ls&amp;loc=1&amp;referer=https://onlinecasino888.us.org   http://birkie.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://www.bunyanmeeting.co.uk/goto-link.php?link_id=19&url=https://onlinecasino888.us.org  http://oraung02.iamgold.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://29wheels.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://solidaritycenters.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://807bbs.com/uchome/link.php?url=https://onlinecasino888.us.org   http://kanariansaaret.net/go.php?go=onlinecasino888.us.org  http://just-what-you-need.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://nutrimlife.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://bbbb444.net/2048/goto.php?url=onlinecasino888.us.org 
http://gracetinos.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.podwiazka.eu/goto.php?url=onlinecasino888.us.org   http://www.arba-makmur.com/redirect.php?link=https://onlinecasino888.us.org   http://hinmanhowardandkattell.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://mahal.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://joyforster.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://qys.daocapital.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://oif.ortax.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.vision.lt/bitrix/rk.php?id=2655&event1=banner&event2=click&event3=9+%2F+%5B2655%5D+%5BBOTTOM3%5D+VV2018&goto=https://onlinecasino888.us.org 
 
 
 
 
 
http://www.borsaekrani.net/index.php?topic=2634.new#new
http://forum.2dar2.de/index.php?topic=127484.new#new
http://jesuslove.asia/forum1/viewtopic.php?pid=891234#p891234
http://consumerstandardng.com/forum/index.php?topic=8828.new#new
http://www.technodom-blog.com/forums/viewtopic.php?pid=91032#p91032