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://xxx1.teen-nudism-pics.info/cgi-bin/out.cgi?ses=HKRSKzoFXz&id=37&url=https://cbdoilamericano.com  https://pinoyeshop.com/go/zalora/?url=https://cbdoilamericano.com   http://www.falsestart.biz/tc/i.cgi?cat=15&mode=redirect&ref_eid=8700&url=https://cbdoilamericano.com   http://www.ewcuw.com/bbs/redirect.php?tid=286&goto=cbdoilamericano.com  https://www.asyl-clinic.kz/bitrix/rk.php?id=4&site_id=ru&event1=banner&event2=click&event3=1+%2F+%5B4%5D+%5Bmain_middle%5D+%D0%9A%D0%BE%D0%BD%D1%82%D0%B5%D0%B9%D0%BD%D0%B5%D1%80+%D0%BD%D0%B0+%D0%B3%D0%BB%D0%B0%D0%B2%D0%BD%D0%BE%D0%B9+-+%D0%9F%D1%80%D0%B0%D0%B2%D1%8B%D0%B9+%D0%B1%D0%B0%D0%BD%D0%BD%D0%B5%D1%80&goto=cbdoilamericano.com   http://www.tandcassoc.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://www.chongwu.cc/home/link.php?url=https://cbdoilamericano.com  http://www.handsurgeryindia.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://chicagolandelectric.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
https://go.newberry.org/page.redir?target=https://cbdoilamericano.com  http://www.marksmanporn.com/crtr/cgi/out.cgi?c=99&s=&u=https://cbdoilamericano.com   http://equalexperience.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://geriatricservicesorganization.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://www.forgetbranding.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://margret.jhn.se/link.php?linkid=19&url=https://cbdoilamericano.com  http://174.127.195.205/bbs/redirect.php?tid=7663079&goto=cbdoilamericano.com  http://chryslerkansascity.biz/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://consiliumbillingservices.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://davegarrett.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://camppeninsulas.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://thedominospizzastand.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://gravitio.net/redir.php?url=https://cbdoilamericano.com  http://www.koinonia.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://www.correx.eu/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://sunwatercompany.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://shms.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://ashrei.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://us.writeaprisoner.com/form-report-web-problem.aspx?a=https://cbdoilamericano.com/  http://tim.mackey.ie/ct.ashx?id=b9184d35-b02d-4ffb-a363-7a0743263e4a&url=https://cbdoilamericano.com   http://impedimed.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://aber-diamonds.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://running79.e-monsite.com/liens/do_redirect/?url=https://cbdoilamericano.com   http://beaute.tv/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://fortlauderdalechristmas.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://e-rentier.ru.arzai.ru/bitrix/rk.php?id=1&site_id=s1&event1=banner&event2=click&goto=cbdoilamericano.com   http://aculoan.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://xkf.intensivecare.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://wardsfranchise.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://mixespowders.com/gepatit-forum/go.php?https://cbdoilamericano.com   http://www.lactose.at/content/link.php?url=https://cbdoilamericano.com/  http://ryefordsucks.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://disabilityworks.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://longs-drugsstores.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://donateyourstocks.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://robiots.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://www.vividedge.com/__media__/js/netsoltrademark.php?d=&x=0&y=0cbdoilamericano.com  https://www.belgbook.be/?url=cbdoilamericano.com   http://primeira.emktsender.net/registra_clique.php?id=TH%7Cteste%7C82893%7C26959&url=https://cbdoilamericano.com   http://emiratesherbals.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://paccess-sourcing.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://images.google.to/url?q=https://cbdoilamericano.com  http://genesisscience.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://americandreamworld.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://pack365yukonok.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://pacersignup.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://nugrowth.digital/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://cssrpg.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://bjtube.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://greenlightfs.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://www.epitbull.ru/.go.php?url=https://cbdoilamericano.com  http://gzfishing.com/home.php?mod=space&uid=6995&do=album&picid=3023&goto=cbdoilamericano.com  http://centralfloridahockeyclub.com/Goto.asp?url=https://cbdoilamericano.com   http://menaassistanceinc.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://mysmartcreditreport.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://quebecicewine.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://galapagosaffairmovie.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://cunardregistry.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://chymistry.cd.lib.hnlat.com/index.php?m=content&c=index&a=show&catid=1013&id=2066&siteid=26&url=https://cbdoilamericano.com   http://dumbcunt-miriam-lazewatsky.xoxohth.com/go.php?url=https://cbdoilamericano.com  http://social-habitat.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://kepler542b.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://blissparty.club/sessions/new?goto=https://cbdoilamericano.com 
https://gimatria.net/redirect.php?target=https://cbdoilamericano.com  http://pertuity.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://www.kiteaddicts.co.uk/mobiquo/smartbanner/welcome.php?referer=http%3A%2F%2Fwww.kiteaddicts.co.uk%2Fshowthread.php%3F16004-XCWeather-forecast-as-19-49-Sun-22-12-13&code=b36c42071b95c0211791bbdc91a7e665&board_url=https://cbdoilamericano.com   http://localillness.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://00h.nodarksuits.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://bpiinsurance.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://www.kauai.gs/__media__/js/trademark.php?d=cbdoilamericano.com  http://www.signalsbinaryoptions.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://canacorporation.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://amsexperts.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://drsuhaspatil.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://aristechglobal.jp/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://www.fullboost.com.au/forum/mobiquo/smartbanner/ads.php?referer=http%3A%2F%2Fwww.fullboost.com.au%2Fforum%2Fshowthread.php%3F8840-FS-SA-Garrett-TO4-S-1050-delivered&code=383c7f0a1d2d0bb10f9695b658ba1800&board_url=https://cbdoilamericano.com  http://let-me-guess.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://fxtradespace.info/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://jaynecastle.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://infinitiofbridgewater.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://finecosimonline.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
 
 
 
 
 
http://foro.comopeces.com/showthread.php?tid=21716&pid=33794#pid33794
https://ovaismehboob.com/2011/09/12/linkedin-authentication-in-asp-net/
http://inquisitor.dk/web/forum/viewtopic.php?f=36&t=77321
http://www.waterpointmapper.org/forum/showthread.php?tid=121086
https://tretho.info/threads/2801704-lady-vols-radio.html?p=36874734#post36874734