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://mymatcutter.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://netitout.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://restaurant-events.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   https://31000773.r.bat.bing.com/?ld=d3XV01XkM4fhnA95h0liZYrzVUCUzm_vMNYTfWmTntOk6QJF0iNTXr09gkohbKGF2yU8fI9cSdcSlE8OjYabp89CxRmKDDedgRzlMvQWnPDFQQAwTMCub-51U_tAXHNwJact5SzAtN2tmKLaXwxgU9FFkLtjk&u=https://cbdoilamericano.com  http://fullereffects.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://harcourthighschool.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://uoatr.tomsk.ru/bitrix/rk.php?id=9&site_id=s1&event1=banner&event2=click&event3=1+%2F+%5B9%5D+%5BSIDEBAR_TEXT%5D+%D0%A1%D0%BF%D1%80%D0%B0%D0%B2%D0%BE%D1%87%D0%BD%D0%B8%D0%BA+%D0%A2%D0%BE%D0%BC%D1%81%D0%BA%D0%BE%D0%B3%D0%BE+%D1%80%D0%B0%D0%B9%D0%BE%D0%BD%D0%B0&goto=https://cbdoilamericano.com  http://www.tulipstarhotel.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://bdsmtubeo.com/go.php?track=dxtMen4MYGpKe0I=&url=https://cbdoilamericano.com 
http://uskitchens.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://cirqueworks.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://aetnaabsenceadvocate.info/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://hgmff.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://www.eliteairlines.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://karkis.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://dangerbdsm.com/cgi-bin/at3/out.cgi?id=95&tag=toplist&trade=https://cbdoilamericano.com  http://soundtrackcollector.org/click.php?url=https://cbdoilamericano.com   http://dofinger.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://epipenatschool.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://114.255.108.171/gate/big5/cbdoilamericano.com   http://etnlight.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://crestron-electronics.info/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://pimpbikerclothing.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://axaeqla.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://www.fleursdefrance.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://noesisdata.info/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://nfpannuity.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://pizapp.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://www.dfscincy.org/default.aspx?p=TrackHyperlink&url=https://cbdoilamericano.com   http://fran-chocolates.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://ddi.ironhorseconcrete.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://rostart.ru/links.php?go=https://cbdoilamericano.com   http://www.nypbtfsb.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://temasekholdingsadvisors.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://highachieversscorecard.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://hardyscolony.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://home.hkesp.com/link.php?url=https://cbdoilamericano.com  http://www.motorpasionmoto.com/redirect?url=https://cbdoilamericano.com/   http://www.seniorpolls.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://ad.startlap.hu/rd.php?id=428704&u=https://cbdoilamericano.com  http://greenbuildingweek.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://animesonlinetk.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://circutor.ru/upload/bx/df9/trans.swf?link=%2Fbitrix%2Frk.php%3Fid%3D27%26amp%3Bevent1%3Dbanner%26amp%3Bevent2%3Dclick%26amp%3Bevent3%3D1%2B%252F%2B%255B27%255D%2B%255BLEFT4%255D%2B%25D2%25F0%25E0%25ED%25F1%25F4%25EE%25F0%25EC%25E0%25F2%25EE%25F0%25FB%2B%25F2%25EE%25EA%25E0%2B%25ED%25E0%2B%25C0%25ED%25E0%25EB%25E8%25E7%25E5%25F0%25F1%26amp%3Bgoto%3Dhttp%253A%252F%252Fwww.analyzers.ru%252Fanalyzers%252Ftransformator%252F&flash_url=cbdoilamericano.com  http://www.cwsbds.com/bbs/connect.php?mod=login&op=init&referer=https://cbdoilamericano.com   http://whatisblueprintmn.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://frontstreetinvestment.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://ebghealthlaw.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://jiaochengbao.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://soluciona.indicator.es/Default.aspx?Returnurl=https://cbdoilamericano.com  http://therestaurantcircle.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://dipenta.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://www.teentel.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://hudsonriver.us/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://anyrim.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://www.hku.hk/www/edm/redirect.php?url=https://cbdoilamericano.com  http://www.stgeorgemoc.com/LinkClick.aspx?link=https://cbdoilamericano.com   http://ambulatorymedicine.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://alaricthegoth.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://losangalex.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://stephenchiatovich.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://bandemanufacturing.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://hip-triggers.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://the-upperroom-store.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://f-score.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://sulma.ge/go.php?url=https://cbdoilamericano.com   http://m.safindit.co.za/sycamore-avenue-treehouse-lodge-guesthouse/redirect?url=https://cbdoilamericano.com   http://sexexpanse.com/cgi-bin/at3/out.cgi?s=75&u=https://cbdoilamericano.com  http://www.ville-ge.ch/web-newsletter/newsletter_vdg/go/qui.php?l=743_2702&c=https://cbdoilamericano.com   http://statedivorcelaws.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://www.alfanika.com/bitrix/rk.php?goto=https://cbdoilamericano.com  http://ketabyab.net/redirect.php?store_id=4&keyword=718&link=https://cbdoilamericano.com   http://zoo-clips.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://www.quarterhorsedirectory.org/redirect.asp?url=https://cbdoilamericano.com  http://www.casajonas.com.ar/goto.php?banner=1&l=https://cbdoilamericano.com   http://savingthestarving.org/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://www.galaxy-cable.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://fourpointsinstitutes.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://airstreamsa.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://www.guide.jewelshop.com.hk/redirect.php?url=cbdoilamericano.com  http://www.smsfilm.be/ept/out.php?f=1&pct=40&url=https://cbdoilamericano.com   http://my24seven.eu/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
http://www.dayfuneralhomes.com/link.asp?l=cbdoilamericano.com  http://moviemagicsuite.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://www.avenueconstruction.co.nz/contact/?subject=House+of+the+Year+Event+2012+Photos+and+Awards&referer=https://cbdoilamericano.com   http://mykidmademodern.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://portlytuxedos.net/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://www.fiolent.ru/go.php?url=https://cbdoilamericano.com  http://adawebrewing.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com  http://learningtreecamps.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com   http://imlivecash.com/__media__/js/netsoltrademark.php?d=cbdoilamericano.com 
 
 
 
 
 
http://poselokgribovo.ru/forum/index.php?topic=59443.new#new
https://khonglottaimusic.com/le-vu/?like_comment=4237
http://lezzetinur.com/corba-terbiye-cesitleri/#comment-40508
https://ingenieriainversa.org/forum/index.php?topic=86203.new#new
https://forum.rf-kraken.com/viewtopic.php?f=4&t=33979