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://q.fbtc.ru/bitrix/rk.php?id=1&site_id=s1&event1=banner&event2=click&goto=playonlinecasino.us.org  http://stolenreward.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://212.118.48.154/SiteDetails.aspx?url=playonlinecasino.us.org   http://metairieguitarlessons.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://magicabula.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://forum.hergunkampanya.com/index.php?thememode=full;redirect=https://playonlinecasino.us.org  http://letsmt.lososcar.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://taqana.net/seo/redirect.php?url=https://playonlinecasino.us.org   http://bibleoilsforhealth.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org 
http://bancosban.net/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://www.boingo.com/leavingBoingo.php?url=https://playonlinecasino.us.org   http://www.furlongpetcremation.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://varsityembroidery.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  https://www.citybee.cz/go/https://playonlinecasino.us.org   http://socialworkerinsurance.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://routco.com/__media__/js/trademark.php?d=playonlinecasino.us.org  http://oldblog.andrewdor.com/go.php?https://playonlinecasino.us.org   http://insyncextra.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org 
http://weavethemovie.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://adambronfman.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://myfabpets.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://fbcsunbury.org/System/Login.asp?id=55943&referer=https://playonlinecasino.us.org  http://alteryo.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://www.fullservicestation.com/__media__/js/netsoltrademark.php?d=&playonlinecasino.us.org  http://schonwelt.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://brianfeury.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://www.sexgold.net/cgi-bin/ucj/c.cgi?url=https://playonlinecasino.us.org 
http://reddressbracelet.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://biblioteksbaser.dk/linkme/?bibliotek=742000&base=redirect&url=https://playonlinecasino.us.org   http://peakhabits.net/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://brookfieldnwt.ca/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://smithdesignview.net/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://frans-chocolates.net/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://theboldindian.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://pornsearch.tv/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://energydrinkdubai.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org 
http://soz.luawiki.com/bitrix/rk.php?id=381&site_id=s1&event1=banner&event2=click&event3=1+%2F+%5B381%5D+%5BHEADER_TOP_BANNER%5D+%D0%94%D0%BE%D1%81%D1%82%D0%B0%D0%B2%D0%BA%D0%B0+%D0%BE%D1%82+200+%D1%80%D1%83%D0%B1&goto=playonlinecasino.us.org  http://tigerguys.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://warmmisthumidifiersreviews.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://www.webplexer.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://ffx3chat.createaforum.com/index.php?thememode=full;redirect=https://playonlinecasino.us.org   http://ceraunavolta.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://www.vranovlet.si/go?https://playonlinecasino.us.org  http://www.66hemi.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://www.momsbusty.com/crtr/cgi/out.cgi?s=65&u=https://playonlinecasino.us.org 
http://taswegians.info/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://dragon-bowling.info/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://givethemhope.net/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://arfinancingllc.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://spradleyconstructionco.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://shorthandtraining.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://arnoldwlau.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://www.imacon-usa.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://ufchoy.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org 
http://intl1percent.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://tatui.sp.gov.br/servicos/redirect?url=https://playonlinecasino.us.org   http://sss.lcyyy.cchgfgtcnrca.lcqti.3.gsr.anonimizing.com/home287/createSession?referer=https://playonlinecasino.us.org   http://www.exitservices.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://www.1csseo.com/wp-content/themes/begin/inc/go.php?url=https://playonlinecasino.us.org   http://peachtreeford.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://arparent.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://glopol.company/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://indylasers.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org 
http://uc.18888.com/link.php?url=https://playonlinecasino.us.org  http://af-edirect.net/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://barnonemail.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://checkmycode.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://pictrac.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://www.hannover-adressbuch.de/click.php?eid=213520&www=https://playonlinecasino.us.org  http://vessal.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  https://www.sitedirsec.com/bbs/redirect.php?tid=1720&goto=playonlinecasino.us.org   http://еёќдєє.net/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org 
http://machimbombo.org/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://texasloot.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://www.heightsfinance.net/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://difeiya.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://myoperatingsystem.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://thelittlegentleman.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://www.austrade.gov.au/PrintToPDF.aspx?url=https://playonlinecasino.us.org  http://www.sublung.com/content/LinkClick.aspx?link=https://playonlinecasino.us.org   http://codyhulk.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org 
http://reexecutiveevent.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://www.mb4biz.biz/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://rubymayer.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://www.roachpalace.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://гѓ’гѓ«гѓ€гѓігѓЏгѓЇг‚¤гѓђг‚±гѓјг‚·гѓ§гѓіг‚є.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org   http://kratosdefense.biz/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org  http://tarahan-machinery.com/eng/go.php?https://playonlinecasino.us.org  http://logi4.xiti.com/go.click?xts=292973&s2=130&p=Centre%20hospitalier&clic=N&type=click&url=https://playonlinecasino.us.org   http://myowncostume.com/__media__/js/netsoltrademark.php?d=playonlinecasino.us.org 
 
 
 
 
 
http://forum.vikingstown.at/viewtopic.php?pid=175755#p175755
http://classifiedsadsnow.online/viewtopic.php?pid=2549915#p2549915
http://forum.flaggenkunde.de/viewtopic.php?f=2&t=3503501
http://www.robaloboatowners.com/RBO/viewtopic.php?f=24&t=1658545
https://www.okademi.com.tr/forums/topic/nursing-diagnosis-for-sepsis/