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://wambco.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.cell-design.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://apnetv.tv/redirector.php?r=https://onlinecasino2018.us.org/   http://osteopathie-fr.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://powergenics.info/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://templeofbelief.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://universalportal.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://taxnegotiationnetwork.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://www.dnafingerprint.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
http://realestatefinancialplanningsystems.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.carmaxautofinance.biz/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://www.tmi-s.com/bitrix/rk.php?id=97&site_id=ke&event1=banner&event2=click&event3=2+%2F+%5B97%5D+%5BBOTTOM%5D+APS-73xxL+Best+in+Test+Winner+468x60+en&goto=onlinecasino2018.us.org   http://cifajr.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://iebroadband.com/LinkClick.aspx?link=https://onlinecasino2018.us.org   http://www.sftkzs.com/wp-content/themes/begin/inc/go.php?url=https://onlinecasino2018.us.org  http://kineticmatrix.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  https://www.abcwpa.org/LinkClick.aspx?link=https://onlinecasino2018.us.org   http://9-11consulting.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
http://www.adiland.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://crystalresults.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://dynamicmap.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://morganariel.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://mobile.yellowpages.com.eg/icon-hit?serviceCode=ITM&language=en&companyId=82534&companyName=QmFuayBBQkM=&url=https://onlinecasino2018.us.org   http://www.gechter.com/jump.php?url=https://onlinecasino2018.us.org  http://likeaprosports.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://yofait.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://oklahomaautoauction.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
http://www.billplante.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.animalrating.com/cgi-bin/out.cgi?id=zoo13y&url=https://onlinecasino2018.us.org   http://peugeot-103.de/forum/redir.php?url=https://onlinecasino2018.us.org   http://iamapainter.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.yeshairremoval.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://gavel.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://understandstroke.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://mmjames.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://sarah-kennedy.co.uk/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
http://www.entergynuclearinc.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://w.cullenthomas.com/guestbook/go.php?url=https://onlinecasino2018.us.org   http://toursandcamps.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://m.ishoes.com.au/scripts/redir.asp?link=https://onlinecasino2018.us.org  http://behindthepalm.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://www.santacruzcountyplanning.com/LinkClick.aspx?link=https://onlinecasino2018.us.org  http://kavitz.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://salvationarmycarib.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://www.farwestoil.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
http://massmutualbrokerage.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://abinternotrab.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://tgt.immediateflood.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://hotelworks.vn/dang-nhap?goto=https://onlinecasino2018.us.org  http://networksolutionsux.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://www.marazulcharters.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://sportsmansinnovations.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://leafshockeydevelopment.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://aprostine.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
http://onenetwork.vg/__media__/js/trademark.php?d=onlinecasino2018.us.org  http://plasticexpress.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   https://lukasjanku.cz/redirect.php?https://onlinecasino2018.us.org   http://usstrategygroup.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.afada.org/index.php?modulo=6&q=https://onlinecasino2018.us.org   http://partnerreinsurance.biz/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://medstarambulance.us/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://thebangaloretimes.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://wanderlustingexpat.com/process/RedirectN?url=https://onlinecasino2018.us.org 
http://zpw.kdrowe.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.grupdes.com/enlaces/glinklnk.asp?url=onlinecasino2018.us.org   http://courseofactionpodcast.libsyn.com/website?referer=https://onlinecasino2018.us.org   http://zanaflexmpc.info/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://gulag.notbad.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://www.coronadoyc.org/default.aspx?p=TrackHyperlink&url=https://onlinecasino2018.us.org  http://ashtangayogamontana.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://tarotreadingsinformation.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://machineryjobs.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
http://zijn.samenresultaat.nl/click.asp?ref=654612&site=9040&type=text&tnb=31&diurl=https://onlinecasino2018.us.org  http://www.galaorganizationfoundation.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://idfirebox.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://www.seawave.jp/cgi/adult/rank.cgi?mode=link&id=3965&url=https://onlinecasino2018.us.org  http://www.cow-head.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://www.chec.com.co/LinkClick.aspx?link=https://onlinecasino2018.us.org  http://www.rakusui.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.qponcoupon.com/view/onlinecasino2018.us.org   http://www.backgroundaustralia.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
http://debtsumo.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.medlive.cn/redirect.php?resource=guide_keyword_search&index=0&url=https://onlinecasino2018.us.org   http://stuartkoenig.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   https://forums.epicgames.com/unreal-tournament-2003-2004/ut2004-level-editing-modeling-skinning/102155-pose?goto=onlinecasino2018.us.org  http://razdelguard.biz-market.ru/jump.php?url=https://onlinecasino2018.us.org   http://resourcelists.falmouth.ac.uk/link?url=https://onlinecasino2018.us.org  https://www.pbcorporate.com.sg/redir.php?url=https://onlinecasino2018.us.org  http://catalogy.com/bin/clickhit.php?url=https://onlinecasino2018.us.org/   http://simplytexas.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
 
 
 
 
 
http://www.whisky-academy.com/forum/showthread.php?tid=6096
http://vestiro.ro/forum/index.php/topic,10841.new.html#new
http://missouridom.org/forum/index.php?topic=8378.new#new
http://projectliferp.com/forum/viewtopic.php?f=18&t=23837&p=65877#p65877
http://support-link.info/blog/2019/01/11/essay-on-software-piracy_p5/#comment-373