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://josephineblanc.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://audicincinnati.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://et.ps.im/?url=https://moderncbdoil.com   http://energymfgparts.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://www.belgro.ru/bitrix/redirect.php?goto=https://moderncbdoil.com   http://sales-finder.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://tastespots.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://qatarlab.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://www.bqao.com/go/go.php?url=https://moderncbdoil.com 
http://www.txfgsales.org/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://wattshumphrey.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://codebreakers-elite.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://keysextube.net/best_out.php?c=31&n=2&e=0&g=3&r=25407735&u=https://moderncbdoil.com  http://kyzyl-online.ru/go/url=moderncbdoil.com   http://wow.filmzitate.info/index-link.php?link=https://moderncbdoil.com  https://liralandcom.frantov.com.ua/bitrix/redirect.php?event1=catalog_out&event2=http%3A%2F%2Fprogresi.com.ge&event3=PROGRESI&goto=https://moderncbdoil.com  http://connecttocongress.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://leetheatreplaza.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com 
http://wcapdd.dina.org/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://beatles.kiev.ua/go/url=moderncbdoil.com   http://contemporaryhome.net/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://yashabab.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  https://www.simost.ru/bitrix/rk.php?id=9&site_id=s1&goto=https://moderncbdoil.com   http://kbchapter11.net/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://www.worldtraveltips.net/redirect.cgi?url=https://moderncbdoil.com/  http://peppergays.com/cgi-bin/crtr/out.cgi?id=66&l=top_top&u=https://moderncbdoil.com   http://sphericalharmonicsrecords.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com 
http://profoodsafety.org/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://bretten.schachvereine.de/gbook133e/go.php?url=https://moderncbdoil.com   http://www.choi-pachi.com/redirect.php?url=https://moderncbdoil.com   https://www.acgdmw.cn/wp-content/themes/begin5.2/inc/go.php?url=https://moderncbdoil.com  http://buildinggreenology.biz/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://olympusvaluationgroup.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  https://www.2007hq.com/forums/showthread.php?goto=moderncbdoil.com  http://jhp.sleepycatguestranch.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://white-society.org/go.php?url=https://moderncbdoil.com 
http://hollywoodglobeawards.org/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://a-okay.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://kjs.newyorkveincenter.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://www.newmilfsex.com/cgi-bin/at3/out.cgi?id=60&tag=toplist&trade=https://moderncbdoil.com  http://ez-forex.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://psycheandimage.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://www.fw-su.de/include.php?path=link&contentid=44&link=moderncbdoil.com  http://dotnetwhatnot.pixata.co.uk/ct.ashx?id=87ee17d0-cc75-4439-9b10-c45d6de8172c&url=https://moderncbdoil.com   http://www.parrish-hare.net/__media__/js/netsoltrademark.php?d=moderncbdoil.com 
http://imprimante.nonore.com/go.php?url=https://moderncbdoil.com  http://sfpto.org/LinkClick.aspx?link=https://moderncbdoil.com   http://jlbwork.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://cleanshred.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://anmc.co.kr/index.php?url=https://moderncbdoil.com   http://broadbandmetrofones.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://гѓЇгѓјгѓ«гѓ‰е·ҐиЉё.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://blueskytechstudios.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://lagosforexexpo.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com 
http://printcalendarsite.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://www.little-hen.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://let-it-bit.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://m.keibayoso.jp/rank/out.cgi?id=corerank&url=https://moderncbdoil.com  http://clevod.net/__media__/js/netsoltrademark.php?d=moderncbdoil.com   https://www.bocawestcc.org/default.aspx?p=TrackHyperlink&url=https://moderncbdoil.com  https://tuul.tv/revive/www/delivery/afr.php?zoneid=2322&referer=https://moderncbdoil.com  http://www.cukjobs.co.uk/redirect.php?source=CSW,V3&target=https://moderncbdoil.com   http://valverepair201.org/__media__/js/netsoltrademark.php?d=moderncbdoil.com 
http://aceelectricinc.net/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://lumberville1740house.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://www.kokoro-web.com/cgi-bin/yomi-search/rank.cgi?mode=link&id=2879&url=https://moderncbdoil.com   http://adultcontemporaryradio.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://amitdeshmukh.co.in/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://digimonlinkz.sugiru.tv/out.php?id=68447&jump=https://moderncbdoil.com  http://little-jumps.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://divemeetinfo.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://skipragsdale.net/__media__/js/netsoltrademark.php?d=moderncbdoil.com 
http://upwarduniversity.org/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://liveateastmain.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://www.comelv.com/plus/redirect.php?id=6902342&url=https://moderncbdoil.com   http://estufafria.cm-lisboa.pt/index.php?eID=fontsizer&size=5&redirect=https://moderncbdoil.com  http://designvista.org/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://uniabroad.ir/go/?url=https://moderncbdoil.com  http://mp4moviez.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://keystonecustomhome.net/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://pabloagave.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com 
http://tokyonodai.net/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://evertecepstest.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://selltextiles.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://giftbasketpos.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://gross.thesources.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com   http://wnh.andrewskurth.co.uk/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://www.commutity.org/__media__/js/netsoltrademark.php?d=moderncbdoil.com  http://scuntime.gq/latest-updates/163/?c=3&e=0&g=3&go=click&r=206130&u=https://moderncbdoil.com   http://save4yourfuture.com/__media__/js/netsoltrademark.php?d=moderncbdoil.com 
 
 
 
 
 
http://tigershop.se/forum/showthread.php?tid=110433
http://conceptsnclarity.com/forum/index.php?topic=143543.new#new
http://www.laboutiquedelautomovil.com/forum.laboutiquedelautomovil.com/showthread.php?tid=10729
http://kalapati.atasteofasia.com.au/index.php?topic=239660.new#new
http://www.cursillouk.org/forum/index.php?topic=211829.new#new