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://www.twobrutal.co.uk/forum/blog.php?b=12&goto=onlinecasino.us.org  http://horrorproject.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://bbs.21art.cn/redirect.php?fid=4&tid=694&goto=onlinecasino.us.org   http://www.sdyojoy.com:1108/uchome/link.php?url=onlinecasino.us.org  http://poconotrails.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://toddcory.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://newmutualist.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://sumikzabrze.pl/redirect.php?action=url&goto=https://onlinecasino.us.org   http://corporatehigh.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
https://www.affordablecebu.com/go?https://onlinecasino.us.org  http://filastylematters.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://bbs.mctalks.com/index.php?c=login&&referer=https://onlinecasino.us.org   http://www.01happy.com/redirect.php?u=https://onlinecasino.us.org  http://www.htzkj.com/wp-content/themes/begin/inc/go.php?url=https://onlinecasino.us.org   http://www.lanebryantcreditcard.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://everythinggoesbooks.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://testdkimhosting.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://www.xpressreg.net/EmailRedirect.asp?rid=30956755&url=https://onlinecasino.us.org/ 
http://foodworldworkersunite.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.ballhockeymontreal.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://myturbines.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://renter-reviews.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.volleyballsonthofen.de/cms/?redirect&url=https://onlinecasino.us.org   http://www.odkupieniemiast.pl/cgi-bin/ucj/c.cgi?url=https://onlinecasino.us.org  http://www.stsaviourhampstead.org.uk/goto-link.php?link_id=12&url=https://onlinecasino.us.org  http://www.wyominghealthmatters.org/redir.php?url=https://onlinecasino.us.org   http://www.tishler.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://www.fatalbert.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://vsp-france.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.geekepisodes.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://capitolford.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://qualityplannedcommunity.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://net.shisu.edu.cn/LinkClick.aspx?link=https://onlinecasino.us.org  http://pearlslife.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://vnf.ortax.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://jaffon.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://digitalmediaeducationlibrary.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://isherweapons.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://enews2.sfera.net/newsletter/redirect.php?id=pd.circolomecenate@gmail.com_0000009927_144&link=https://onlinecasino.us.org   http://balmolod.ru/link.php?url=https://onlinecasino.us.org  http://ad.itweb.co.za/adclick.php?bannerid=30982&zoneid=&source=&dest=https://onlinecasino.us.org   http://bestnightinnmotel.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://abetterengineeringjob.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://www.ddlang.com/wp-content/themes/begin-lts/inc/go.php?url=https://onlinecasino.us.org   http://www.aneta.dev.senzaparty.net/redir.php?link=https://onlinecasino.us.org 
http://www.avadl.me/goto/https://onlinecasino.us.org  http://www.edi-software.biz/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://bluearrowone.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://niconese.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://599salon.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://havrehilinerealty.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://amazon4a.appspot.com/www.bats.org.uk/download_info.php?id=1181&file=Halloween_Fundraising_for_Bats.pdf&referer=https://onlinecasino.us.org  http://blueyarn.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://lkv.ptarmigan.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://omgeoconnect.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.phaser.me/wp-content/themes/begin/inc/go.php?url=https://onlinecasino.us.org   http://www.hotwebamateurs.com/cgi-bin/tlists/top100/out.cgi?id=Andee&url=https://onlinecasino.us.org   http://alkahlout.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://zapazit.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://grossepointemag.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://nilesandcompany.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.afn360.com/go-to-source.php?url=https://onlinecasino.us.org   http://adroitlaw.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://tuning-garage.ru/go.php?https://onlinecasino.us.org/  http://www.grandslamfantasytennis.com/news.php?newsaddress=https://onlinecasino.us.org   http://shoppinginsights.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://job-application-online.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://avto-for.ru/forum/go.php?https://onlinecasino.us.org/   http://blog.siol.net/agregat/redirect_post/?url=https://onlinecasino.us.org  http://donungonline.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://saora.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://174133862.r.bat.bing.com/?ld=d3a1zNWdlDeflMQNcWmUS-tjVUCUxhjvc6AtqIf_Wl7AsmDX8hMdKmXZIYb3snSL3QDrb3B7IF_DljSjepJr6sPsO6kRBmW9EwlZtWErzDGiCvk6pghprkGF6gnYFayHfV8b6WTOF0pzRFO_IwXg0sV_gCdACYd4L4bri_sBN69ryOSmf9&u=https://onlinecasino.us.org 
http://www.homepageexpo.com/redirect.php?url=https://onlinecasino.us.org  https://www.findproductmanuals.com/go.php?link=https://onlinecasino.us.org   http://radiocommunications-usa.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://esteronews.biz/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://hardtickets.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://usedprivateplanes.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://jqp.tippytoe.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://728.familydollar.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://ppcoverview.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://juleslennon.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://georgewbushfreedomcenter.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://lww.coreskatemag.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://ihategreatlakes.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.dmzy8.com/wp-content/themes/begin4.6-1/inc/go.php?url=https://onlinecasino.us.org   http://nautilus-gurzuf.ru/go.php?url=https://onlinecasino.us.org  http://a6forum.pl/mobiquo/smartbanner/ads.php?referer=http%3A%2F%2Fa6forum.pl%2F2-0-tdi-cr-vcds-wypalanie-dpf-a-t2174.html&code=28fc132629cfec526a928078ec160118&board_url=https://onlinecasino.us.org  http://sambastore.com/en/mandeparaumamigo.php?url=https://onlinecasino.us.org   http://techticket.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
 
 
 
 
 
https://kokifilmaziureti.lt/2016/07/24/we-need-to-talk-about-kevin-2011/
http://niklever.com/spritoon/forum/viewtopic.php?f=5&t=349839
https://weechookeong.com/2018/05/30/masa-sekarang-menungggu-mu-part-5-tourism-msias-rm99-694-mil-speedy-gonzales-contract/
http://chs.my/forum/viewtopic.php?f=4&t=90705&p=285209#p285209
http://alanlauwatch.com/forum/viewtopic.php?f=2&t=177058