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://lgbtcounts.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://sanantoniodoctorsoncall.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.gwatcht.com/home/link.php?url=https://onlinecasinodd.com   http://www.iaudit.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.withubtraining.org/kefu.htm?arg=withubtraining&style=1&kf=&kflist=on&kf=cisco2%2Ccisco1%2Cadobe%2Cgongye%2Cofficezfbt%2Coffice%2Ctese%2Cjava%2Citen%2Cbigdata%2Cibm%2Cqiye%2Cwithub%2C706358385%2C1580263851%2C19678429&zdkf_type=1&language=cn&charset=gbk&username=&userinfo=&introurl=&lyurl=&lytype=0&copartner=&referer=https://onlinecasinodd.com   http://24news.lt/go.php?url=https://onlinecasinodd.com  http://clearwaterchildrenshealth.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.oktotech.com/wp-content/themes/Grimag/go.php?https://onlinecasinodd.com   http://kocolenemarketing.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://andrewharpershideawayreport.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://abcnews.go.com/cgi/atransfer.pl?name=36981&srvc=nws&guid=none&goto=https://onlinecasinodd.com   http://chickintech.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.inmir.de/cms/?redirect&url=https://onlinecasinodd.com  http://waterstreetcommunityinfo.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://praguevoyages.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://flack.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://tlrc-en.tbzmed.ac.ir/?pageid=1&MID=22&url=https://onlinecasinodd.com   http://discountcodehotels.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://czf.edacinc.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://searchandtreat.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.lifetree.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://oce.ssiwebonline.com/track.aspx?a=3656&r=32&c=&u=https://onlinecasinodd.com  http://crispinteriors.ca/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.jessicab.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://rhinestonenames.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  https://www.bwyc.org/default.aspx?p=TrackHyperlink&url=https://onlinecasinodd.com   http://harrisbales.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
https://42039326.r.bat.bing.com/?ld=d3UKkvyXE9ybClzux4tva3jTVUCUwXelkh2bdYJgSV-eOXMMdlTar8tVT4k4EEg4VU3DZMuxNMvP_gBUhpq4dQwO8IC6yxawbaqgFmYF43d-LRjceQo6SWycih8RbhA7VuMqGtLO40nwmx90Qfn_RT11fZpOqB6rP4Uw00ODgBGtj5TnWq&u=https://onlinecasinodd.com  http://informedigital.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://okidataorders.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://angel-kazan.ru/go.php?url=https://onlinecasinodd.com  http://midwesternmarketing.co/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://sawmillholdings.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://afandbbuckhead.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://slipn.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://savingswithpawraylah.info/click.php?id=153&url=onlinecasinodd.com 
http://home.shipai.net/link.php?url=https://onlinecasinodd.com  http://pashminaworld.biz/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://movetoamend.nationbuilder.com/r?u=https://onlinecasinodd.com   http://walkforrecovery.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://bsj.internethostpilot.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://imc-holdings.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://somersetonelizabeth.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  https://bbs.lihuasoft.com/redirect.php?tid=3984&goto=onlinecasinodd.com   http://dcdowell.us/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://hobnobblegobble.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://searcherama.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://eneonatalhealth.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://analystz.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://directholdingsamericas.us/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://hastingsautobody.com/gallery/main.php?g2_view=core.UserAdmin&g2_subView=core.UserLogin&g2_return=https://onlinecasinodd.com/  http://lyrictalent.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://stgau.ru/bitrix/rk.php?goto=https://onlinecasinodd.com/   http://mix-hub.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://hiskbc.com/includes/go.php?url=onlinecasinodd.com  http://myappshubb.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://nationstarhouseloan.info/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://infotronicsonline.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://globalezlink.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://skylightfinancial.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://athensdream.gr/index.php/component/k2/item/7-the-beables-eng?fromurl=onlinecasinodd.com  http://firsteyeomaha.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://hocc.cc/forum/redirect.php?fid=32&tid=41593&goto=onlinecasinodd.com 
http://flipflippen.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://enikos.smart-ads.gr/adclick.php?bannerid=2547&zoneid=0&dest=https://onlinecasinodd.com   http://pokerzion.com/Poker_Forum/showthread.php/16300-S%C3%A1%C2%BB*a-tivi-t%C3%A1%C2%BA*n-nh%C3%83*-gi%C3%83%C2%A1-r%C3%A1%C2%BA%C2%BB-uy-t%C3%83*n?goto=onlinecasinodd.com   http://womenshealthfirst.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://m.forum.detik.com/showthread.php?t=1593562&goto=onlinecasinodd.com   http://www.yoga.healthyhuman.net/go.php?url=https://onlinecasinodd.com  http://kcrels.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://dlib.ischool.drexel.edu/gsdl/cgi-bin/library.cgi?e=d-00000-00---off-0cybulsk1--00-1----0-10-0---0---0direct-10---4-------0-1l--10-gl-50---20-help---00-3-1-00-0--4--0--0-0-01-10-0big5-10&a=extlink&rl=0&href=https://onlinecasinodd.com   http://www.braziliansbeautysecrets.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://apps.trutube.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://diningreservation.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://risingstarscholars.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://trescerocinco.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://mazaktradein.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://bbwmilftube.com/cgi-bin/bmt.cgi?c=1&s=70&bmt=1&u=https://onlinecasinodd.com  http://rcaonline.info/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://momporntv.net/go.php?ma=17&tu=25&re=1688&url=https://onlinecasinodd.com   http://dcpracticeinsights.com/mpacms/dc/pi/link.php?p=492&c=1392&c_url=https://onlinecasinodd.com 
http://usedcallawayclubs.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://pardonmepeople.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://slateroofstlouis.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://blq.deadromans.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://beldentristatebuildingmaterials.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://doylehotel.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.paywei.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://metro-ny.us/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.ddc.moph.go.th/banner/showimgbanner.php?id=72&url=https://onlinecasinodd.com 
 
 
 
 
 
http://taiwan.yam.org.tw/womenweb/bookclub/forum1/edit-response.cgi/womenweb/bookclub/forum1/get.cgi/newreply.php
http://www.tunturisusi.com/aforismitfoorumi/phpBB3/viewtopic.php?f=13&t=11479
http://ttmicro.com/forum/index.php?topic=159322.new#new
https://lavoroabergamo.it/index.php?topic=507267.new#new
https://weechookeong.com/2018/05/30/masa-sekarang-menungggu-mu-part-5-tourism-msias-rm99-694-mil-speedy-gonzales-contract/