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://wosser.getfreegoods.org/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://bficanada-quebec.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://videodw.comin.videodw.com/url?q=https://casinorealmoney.us   http://www.mineralplaster.co.nz/ra.asp?url=https://casinorealmoney.us  http://georgetownpartners.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://mohammadsaid.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://s5.shinystat.it/cgi-bin/redir.cgi?URL=https://casinorealmoney.us  http://www.vanpeople.com/space/link.php?url=https://casinorealmoney.us   http://bioe.com/exit.aspx?URL=https://casinorealmoney.us 
http://mappk.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://investinchinesecompanies.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://p-bot.com/cgi-bin/atx/out.cgi?id=16&tag=top&trade=https://casinorealmoney.us   http://gamespider.co.uk/redirect.php?retailer=0&deeplink=https://casinorealmoney.us  http://shop-europe.proceq.com/redirect.php?action=url&goto=casinorealmoney.us   http://globalgolfpro.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://www.madagascar.net/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://rpx.a8.net/svt/ejp?a8mat=1U7G6P+8S5702+2HOM+BW8O1&rakuten=y&a8ejpredirect=https://casinorealmoney.us   http://www.smutgranny.com/cgi-bin/atx/out.cgi?id=316&tag=toplist&trade=https://casinorealmoney.us 
http://www.haku.com.tw/redirect.php?tid=6485&goto=casinorealmoney.us  http://www.cadenashoteleras.org/log.es.html?e=39&r=3&u=https://casinorealmoney.us   http://www.kingsrestaurants.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://www.deepfrieddiesel.net/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://bodybuildingcoach.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://mhcbuyp.libsyn.com/?referer=https://casinorealmoney.us  http://www.gs-sochi.ru/bitrix/rk.php?event1=file&event2=download&goto=casinorealmoney.us  http://comic-checker.herokuapp.com/check_update/redirect?url=https://casinorealmoney.us   http://wearehcrmanorcare.net/__media__/js/netsoltrademark.php?d=casinorealmoney.us 
http://www.freesexycontent.com/crtr/cgi/out.cgi?id=146&l=t2&u=https://casinorealmoney.us  http://www.sgsl.info/Goto.asp?url=https://casinorealmoney.us   http://www.newyorkhotelguide.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://citadelmgmt.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://remotecontrolinc.net/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://www.sport4all.be/redir.php?titel=Elise+Mertens+opent+tegen+Roemeens+reekshoofd+Niculescu&link=https://casinorealmoney.us  http://jqm.gatematic.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://www.target.co.il/site/linkclick.aspx?link=https://casinorealmoney.us   http://www.bvrefrigeration.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us 
http://huntingtonforschool.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://zarzadzanie.uni.lodz.pl/LinkClick.aspx?link=https://casinorealmoney.us   https://www.hosting22.com/goto/?url=casinorealmoney.us   http://www.5168.biz/home/link.php?url=https://casinorealmoney.us  http://stydentki.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://marineblastvideos.co.uk/NetServe/KL42P0U_mallSsoAction.do?goto=casinorealmoney.us  http://wallboardrecycling.org/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://panamaexpeditions.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://realadvice.pro/__media__/js/netsoltrademark.php?d=casinorealmoney.us 
http://www.yellowpages.eindiabusiness.com/url.php?iid=https://casinorealmoney.us  http://lgpmanagement.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://nationaldancesportcorporation.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://scartreatment.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://vgu.mailprocessing.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://www.tecnipublicaciones.com/amdpress/redirect.asp?ID=34123&url=https://casinorealmoney.us  http://theprologuesociety.info/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://www.unleashedadventures.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://hisnewsnetwork.org/__media__/js/netsoltrademark.php?d=casinorealmoney.us 
https://schelterofficeplus.ca/track.aspx?a=3807&r=45&c=&u=https://casinorealmoney.us  http://dfwrugcleaning.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://defectivemen.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://www.hits.cdd.nhs.uk/HIT/MoreDetailsv2.jsp?id=11795&dsn=hphit&referrer=https://casinorealmoney.us/  http://co.adams.wi.gov/LinkClick.aspx?link=https://casinorealmoney.us   http://westerntires.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://leebiz.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://secondsaturdaysa.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://raguweb.net/antena/html/link.php?url=https://casinorealmoney.us 
http://www.1800newbasement.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://www.wauwatosanow.com/s?action=editReg&rurl=https://casinorealmoney.us/   http://www.shadyfamily.net/blank.php?path=content/links.php&contentid=103&link=casinorealmoney.us   http://mylastrequestbox.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://www.smartbabysitting.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://www.hklawpage.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://aapg.biz/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://www.floridaforensics.org/__media__/js/trademark.php?d=casinorealmoney.us   http://kurdmusik.hostpost.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us 
http://imgworldwide.info/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://primorsky.ru/bitrix/rk.php?id=111&event1=banner&event2=click&event3=1+%2F+%5B111%5D+%5BRSB_BLOCK_2%5D+%D0%A1%D0%BF%D0%B8%D1%81%D0%BE%D0%BA+%D1%82%D0%B5%D0%BB%D0%B5%D1%84%D0%BE%D0%BD%D0%BE%D0%B2+%D0%B3%D0%BE%D1%80%D1%8F%D1%87%D0%B8%D1%85+%D0%BB%D0%B8%D0%BD%D0%B8%D0%B9&goto=https://casinorealmoney.us   http://www.oltreverso.it/home/wp-content/plugins/spfeeds/controllers/rs.php?f=&url=https://casinorealmoney.us   http://go.cin7.com/ra.asp?url=https://casinorealmoney.us  http://www.him3.ru/redir.php?url=https://casinorealmoney.us   http://onlycelebsfakes.com/cgi-bin/crtr/out.cgi?id=43&l=top_top&u=https://casinorealmoney.us  http://kreatif.com.ua/redirect.php?https://casinorealmoney.us  http://cheaptrafficschoolfast.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   https://parmanziani.it/modules/mod_jw_srfr/redir.php?url=https://casinorealmoney.us 
http://liaudiology.net/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://firstchoicerates.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us   http://www.homeanddecor.sg/blogs/tags/LDAP_PATH/fbLogin.html%3Bjsessionid%3D4B84E3D04D5E5CA62FB53E30D305F2CC?page=4&module=facebook&svc=hnd&goto=https://casinorealmoney.us   http://www.beast-art.com/cgi-bin/out.cgi?id=animalt7&url=https://casinorealmoney.us  http://stanford.ebookhost.net/ssir/digital/22/ebook/1/scripts/redirect.php?url=https://casinorealmoney.us   http://tubularsteel.com/__media__/js/trademark.php?d=casinorealmoney.us  http://www.milamstudios.com/__media__/js/netsoltrademark.php?d=casinorealmoney.us  http://www.cocogd.com/home/link.php?url=https://casinorealmoney.us   http://repevaluator.biz/__media__/js/netsoltrademark.php?d=casinorealmoney.us 
 
 
 
 
 
http://www.capitolasset.net/forum/showthread.php?tid=5496
https://www.hidradenitis-suppurativa-forum.org/viewtopic.php?f=6&t=312
http://www.truck-thai.com/forum/index.php/topic,929.new.html#new
https://ingilizceyehazirlik.com/showthread.php?tid=10606&pid=14889#pid14889
http://fakefes.hopto.org/index.php?topic=1195.new#new