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)

https://www.negritella.it/sito/external_url.php?url=https://onlinecasino888.us.org  http://veteranssearch.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://convergesecurity.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://mountainsprings.church/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.businessloanpune.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://384.myeducationworld.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  https://1122124.r.bat.bing.com/?ld=d3ftOcgEQZQ7NKp04DfW64CDVUCUxYlS4YkUe42heMTr0lb-IYFK1dr4bR4DMi3CENr-vaO8jNXdtDrTJj-fVNF265SnHuqr3CtDpjeE10UxMYjP6Ueoatg0uGdaVGbNMRBhRw1CaDMKeJ6h-P6Q2j99yjZOI&u=https://onlinecasino888.us.org  http://www.systematicatrading.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://wetsand.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://hillbillycentral.co/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://kenmink.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://farsitopz.ir/redirect?url=https://onlinecasino888.us.org   https://www.performia-cis.ru/bitrix/redirect.php?event1=Banner-Test&event2=&event3=&goto=https://onlinecasino888.us.org  http://vkw.geminisystemsllc.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://kennethwyattgalleries.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  https://gwdang.com/union/go/?site_id=228&target_url=https://onlinecasino888.us.org  http://pestquip.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://meetinghouses.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://nfpcanada.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://dishfootball.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://pfchangs-coupons.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://ipodcatalogs.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.becomeascot.org/redirect?SessionGuid=0035ccb7-52a0-4f77-a043-f52c7c891a59&url=https://onlinecasino888.us.org   http://royaldompedro.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://forum.lightray.ru/link.php?url=https://onlinecasino888.us.org  http://www.clublamorada.com/default.aspx?p=TrackHyperlink&url=https://onlinecasino888.us.org   http://ganjagourmet.co/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://2013.mudconnect.com/cgi-bin/redirect.cgi?mud=Endless+City&url=https://onlinecasino888.us.org  http://www.rpklublin.pl/skins/rpk/redirect.php?url=https://onlinecasino888.us.org   http://sbphototours.com/includes/companyLogo.php?url=https://onlinecasino888.us.org/   http://partnerrelife.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.madagent.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://wapsows.com/go/https://onlinecasino888.us.org  http://jlc2.qxjy.com/redirect.aspx?url=https://onlinecasino888.us.org  http://www.jbjenkinsfuneralhome.com/cfi/addthis_email.cfm?url=https://onlinecasino888.us.org   http://urbanbowl.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://hadsy.net/ct.ashx?id=c4305098-4327-4301-a952-52858bf016cd&url=https://onlinecasino888.us.org  http://talmudstudy101.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://anchordock.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://veter-vbo.ru/go?https://onlinecasino888.us.org  https://www.360sportsinc.com/modules/mod_jw_srfr/redir.php?url=https://onlinecasino888.us.org   http://start.earthlink.net/track?linkid=1015976&add=1&url=https://onlinecasino888.us.org  http://gangstacaraudio.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://ginpa.net/link/rank.php?url=https://onlinecasino888.us.org   http://keysfood.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://geovirtual.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://eccl.es/index.php?url=https://onlinecasino888.us.org   http://www.jmfuneralhome.com/cfi/addthis_email.cfm?url=https://onlinecasino888.us.org   http://theamericanlegacyguitar.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://brutalcycles.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://rocklance.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://resilientnetworks.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://novationmanufacturing.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.zenkokuren.org/zcgi/zmt/mt4i.cgi?id=2&cat=24&mode=redirect&no=9&ref_eid=375&url=https://onlinecasino888.us.org 
http://goglle.coo123.net/goto.php?c=&n=standard&url=https://onlinecasino888.us.org  http://cervicalstim.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://stthomasfcu.com/redirect.php?link=https://onlinecasino888.us.org/   http://sunsoakersshingles.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.amateurwebcamz.com/cgi-bin/autorank/out.cgi?id=naughtyc&url=https://onlinecasino888.us.org   http://likegettingmoneyinyourmailbox.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://brassconstruction.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.gonadz.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   https://61000018.r.bat.bing.com/?ld=d3bXRXy9qdESa4I0ijJWIUsTVUCUySAv9FxyiDEc48y7OQpwIwTDikCercs0drgPutcwqHkvfszRqNSt_pnQk5ant1c2L5gKZnJbb8iBBLsm_SKdLKxn9HQ26CZBt32NejsNdUV549K-0rXHitAvMEvwvoxkDg134Z2sgFiQHi8Bu6VVau&u=https://onlinecasino888.us.org 
http://parentsofla.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://lasvegasnonstop.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://гЃ¦гЃ„гЃг‚“.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://xiuang.tw/debug/frm-s/onlinecasino888.us.org/  http://pizzicommunicationscompany.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://lifestrawsales.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://citystar74.ru/go?to=https://onlinecasino888.us.org  http://cafekala.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://gonzito.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://fracbullet.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.paintsprayerguru.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.pegasuscycling.nz/LinkClick.aspx?link=https://onlinecasino888.us.org   http://www.2ndnewhawscouts.com/gallery/main.php?g2_view=core.UserAdmin&g2_subView=core.UserLogin&g2_return=https://onlinecasino888.us.org  http://www.iiidesign.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://vivianahand.com/m/redirect.php?url=https://onlinecasino888.us.org  http://djarumsuper.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://univarchinaltd.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://comoadelgazarlaspiernas.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://www.jugon.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.xknull.com/ept/out.php?f=1&pct=50&url=https://onlinecasino888.us.org   http://av-list.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://mverdu.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://preplearning.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://collectorsystem.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  https://www.tenpojimusyo.jp/schedule/cgi-bin/info.php?a<>=<a+href=https://onlinecasino888.us.org  http://nerozone.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.basketbolist.org.ua/go.php?url=https://onlinecasino888.us.org 
 
 
 
 
 
http://www.technodom-blog.com/forums/viewtopic.php?pid=93562#p93562
https://giikah.com/forum/index.php?topic=7875.new#new
https://hebreu.net/question/life-quest-free-trial/
http://test.rm-reklama.ru/showthread.php?p=183353#post183353
http://vonwolkenstein.ano-host.co.in/vb4-neu/showthread.php?400503-famous-mage-names&p=545867#post545867