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://bible-maps.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://kupi-proday.by/redirect/?go=https://onlinecasino.us.org   http://carnetquicklist.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://fr.99quiz.com/link.php?link=https://onlinecasino.us.org  http://bizar-media.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://cgitemps.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://141027891.r.bat.bing.com/?ld=d3zb_jeb5TBYGwGSeSAOJVozVUCUyfMBVVCywWDYUXBqG8996YX3l0iJCy-NGmfGpckRA_AQL0g5saROGMIobpY5u4KD7Zso6JeUXjbRpjHNE_tD_dt5NJN8xRjNF5Lt1d8jnlA0mf1mQcxjkkhsoNfjd0eJ3lWko_nCXjFN_AZ9O832lf&u=https://onlinecasino.us.org  http://nuclear.expert/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://gratui-tees.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://www.discoveruci.net/redirect?SessionGuid=135c4e89-a983-4f77-9143-daf046534533&url=https://onlinecasino.us.org  http://dwf.undercovercamera.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://448shill.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://app.meltwater.com/mwTransition/?url=https://onlinecasino.us.org  http://contractassay.cn/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://dreamflute.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://tradeext.us/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://designingbanks.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://go.bodogame.com/index.php?url=https://onlinecasino.us.org 
http://iread4fun.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.awccc.com/wp-content/themes/begin/inc/go.php?url=https://onlinecasino.us.org   http://tripodphoto.ru/go?https://onlinecasino.us.org   http://getagripdefense.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://umarket.be/go?https://onlinecasino.us.org   http://www.0934088843.tw/pro/redirect.php?url=https://onlinecasino.us.org  http://1877findout.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://brinkslights.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://networkrecovery.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://www.maturemoviesworld.com/cgi-bin/at3/out.cgi?id=58&tag=toplist&trade=https://onlinecasino.us.org  http://ceskapozice.lidovky.cz/redir.aspx?url=https://onlinecasino.us.org   http://happyhellowork.clipe.pt/go.php?url=onlinecasino.us.org   http://zechman.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://how2vacation.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://garykain.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://oneperfectpilsner.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://rusticislands.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://equalafh.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://scriptsmafia.gemandjewelryguide.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://lezlez.ranks1.apserver.http21.com/bestofadult/mkr/out.cgi?id=07385&go=/onlinecasino.us.org   http://www.tributetodeanmartin.com/elvis/go.php?url=https://onlinecasino.us.org   http://integratedwaterworks.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.thebestpageintheuniverse.net/outgoing.cgi?u=https://onlinecasino.us.org   http://mcindustrial.biz/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://dzhig.com/blog-o-rybalke/item/26-content-demo-135/26-content-demo-135?goto=onlinecasino.us.org  http://www.jig.jp/redirect/?url=https://onlinecasino.us.org   http://333.townegallery.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://aapm.org/r/?s=AUTO&m=4573&u=https://onlinecasino.us.org  http://goneaskew.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://penn-exec.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.fidelityflatheadcounty.com/customRedirect.aspx?url=https://onlinecasino.us.org  http://www.gaitclinical.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://blue11records.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.hermite.org/wp-content/plugins/go.php?https://onlinecasino.us.org/  http://coredaptx.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.ausight.com.au/oscommerce/redirect.php?action=url&goto=onlinecasino.us.org 
http://daliprints.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://nbc6weatherplus.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://bathroomdetailing.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.falconpro.biz/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://usbkey4u.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://cousinsproperties.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://lauramatter.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://zusds.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://reseau-anact.fr/emailer/newsletters/stats/link.jsp?sending=2123&id=475673&url=https://onlinecasino.us.org/ 
http://hockeymanawatu.org.nz/LinkClick.aspx?link=https://onlinecasino.us.org  http://mura.us/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://cordlocks.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://poleover.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.psirep.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.coverti.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.logisticsintegration.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.markclark.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.westportnow.com/index.php?url=https://onlinecasino.us.org 
http://mattbakey.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://memberservices.membee.com/feeds//login/redirect.aspx?cid=573&desturl=https://onlinecasino.us.org   http://kosaircrippledchildrenhospital.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.blueprintforcash.com/go.php?url=https://onlinecasino.us.org  http://av-list.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://www.free521.com/wp-content/themes/begin/inc/go.php?url=https://onlinecasino.us.org  http://www.4x4opony.pl/cgi-bin/redirect.pl?https://onlinecasino.us.org  http://vision-core.info/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://pestguru.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://dreamsillustrated.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://parillogroup.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://kobymanufacturingcompany.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.concours-emploi.com/r.php?u=https://onlinecasino.us.org  http://ftlauderdalesightseeingcruise.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.sup-power.com/Chat.htm?arg=10004226&style=1&kf=&kflist=on&kf=jackyro%40led-card.com%2Csales033%40led-card.com%2Csales032%40led-card.com%2Csales031%40led-card.com%2Csales036%40led-card.com%2Csales035%40led-card.com%2Csales037%40led-card.com%2Csales038%40led-card.com%2Csales039%40led-card.com%2Ceng021%40led-card.com%2Ctoby%40led-card.com%2Csales021%40led-card.com&zdkf_type=1&language=en&charset=gbk&username=&userinfo=&introurl=&lyurl=&lytype=0&copartner=&referer=https://onlinecasino.us.org  http://www.stocking-glamour.com/cgi-bin/at3/out.cgi?id=163&trade=https://onlinecasino.us.org  http://syfir.co/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.animalrank.com/cgi-bin/out.cgi?id=adraw&url=https://onlinecasino.us.org 
 
 
 
 
 
http://diamondrpg.tk/forum/viewtopic.php?f=68&t=945&p=33770#p33770
http://www.gsusmusic.org/viewtopic.php?f=2&t=193791&p=253986#p253986
http://seointelligenceagency.com/forum/viewtopic.php?f=103&t=4377&p=348597#p348597
http://coconut-water.in/index.php?topic=236401.new#new
http://forum.lib42.ru/viewtopic.php?pid=70901#p70901