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.visitfaidellapaganella.it/sito/external_url.php?url=https://onlinecasino888.us.org  http://freelessonplanshub.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://readyhosting.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://costumecase.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://portlandcareersonline.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.americaninn.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://orijikan.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://patrickstgermain.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://blog.labtoday.net/link.php?url=https://onlinecasino888.us.org 
http://boekingsassistent.be/out.php?id=r36_langsdekust_nl&appl=31&link=https://onlinecasino888.us.org  http://miahamilton.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://downholenergy.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://silkroad.co/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://ventred.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.illiante.com/ct.ashx?id=f4c9abdf-2d0b-4c61-b422-1c56790f23b2&url=https://onlinecasino888.us.org  http://www.tubepornx.com/cgi-bin/atx/out.cgi?id=1112&trade=https://onlinecasino888.us.org  http://gardentutoronline.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://bestvideorentals.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://pcmag.ru/bitrix/rk.php?id=2168&event1=banner&event2=click&event3=25+%2F+%5B2168%5D+%5B1000_60%5D+Treolan+Ultrabook&goto=https://onlinecasino888.us.org  http://ydv.lifeflourgroup.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://seo-akademie.com/website-check/redirect.php?url=https://onlinecasino888.us.org   http://ch1.artemisweb.jp/linkout.cgi?url=https://onlinecasino888.us.org  http://elias.ztonline.ch/?a<>=<a+href=https://onlinecasino888.us.org   http://www.patprikkel.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://computerintegratedsolutions.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://sevendaughterswine.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.domodedovo-city.ru/plug.php?e=go&url=https://onlinecasino888.us.org 
http://krpole.ru/bitrix/rk.php?goto=https://onlinecasino888.us.org  https://webmail.unige.it/horde/util/go.php?url=https://onlinecasino888.us.org   http://www.zulic.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.rmrpehsu.org/LinkClick.aspx?link=https://onlinecasino888.us.org  http://robertshulevitz.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.youngswirls.com/cgi-bin/crtr/out.cgi?id=519&l=top_top&u=https://onlinecasino888.us.org  http://allthisiskc.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.ktd.ro/ktd_exit.php?goto=https://onlinecasino888.us.org   http://medingenuity.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://jga.parshav.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://boatsbootsandbullets.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://akashicfieldclearing.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://soldertip.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://nvtt.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://ourmidland.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://tradeext.us/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://takeonthenews.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://sevaenergy.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://m-grup.derevo.ua/redirect?goto=https://onlinecasino888.us.org  https://dlhsoft.com/GoTo.aspx?url=https://onlinecasino888.us.org   http://hairypussyparty.com/cgi-bin/at/out.cgi?s=60&c=1&u=https://onlinecasino888.us.org   http://calpinewindfarms.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://02babc5.netsolhost.com/guestbooks/GChiswell/guestbook.html   http://nextgenusersmeeting.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.stonefilms.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.financeseek.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://fixdot.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://zagnc.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  https://www.gzs.si/LinkClick.aspx?link=https://onlinecasino888.us.org   http://familybusinessoftheyearaward.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.xpresstrack.eu/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.globaldenturepartners.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://insulationhq.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://recoverythroughsupport.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://allstonhotel.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://bankdirectories.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://lbj.drive-thrukaraoke.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.brucegreene.com/SearchPoint/redir.asp?reg_id=pTypes&sname=/searchpoint/search.asp&lid=0&sponsor=LEA&url=onlinecasino888.us.org   http://www.jacobigroup.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://10.xg4ken.com/media/redir.php?prof=362&camp=9110&affcode=cr16088&k_inner_url_encoded=1&cid=3280371390|699906|try big city adventure&mType=b&queryStr=~play flash games&kdv=c&url<>=https://onlinecasino888.us.org  http://renaissancelasvegas.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://northernmagnolia.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://one1099.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://koshcheev.ru/redir.php?url=https://onlinecasino888.us.org   http://newdirectionconsulting.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://wlex18.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://cawalters.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://174.127.195.205/bbs/redirect.php?tid=7663079&goto=onlinecasino888.us.org   http://www.thejournal.ie/utils/login/facebook/?url=https://onlinecasino888.us.org  http://sauna-bath.instant-sales.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://vegeties.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://policerecords.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://nwtexaschildrenshospital.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.mononacatering.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://www.ongbetting.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://1hourtshirts-kc.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://gunlawfirm.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://bluethunderparts.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  https://onlinecasino888.us.org.acs.hcn.com.au/   http://www.unitedscreening.tw/__media__/js/trademark.php?d=onlinecasino888.us.org  http://cel.ncu.edu.jm/LinkClick.aspx?link=https://onlinecasino888.us.org  http://matalda.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://solidaritycenters.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
 
 
 
 
 
http://graphfolio.com/forum/index.php?topic=12995.new#new
https://forums.dpi.nsw.gov.au/viewtopic.php?f=2&t=1252
http://andhrabay.com/forum/index.php?topic=13399.new#new
http://khalifatullahmehdi.info/forum/index.php?topic=10539.new#new
http://forum.oranis.net/viewtopic.php?f=3&t=4370