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.black-board.net/thread.php?goto=onlinecasino.us.org  http://aetnaabsenceadvocate.co/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.zeograd.com/goto.php?https://onlinecasino.us.org   http://assuredpartnersnl.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://mayfairforum.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://enbmobile.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://wheatland-tube.us/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://engelandvolkersoakville.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://purelightingllc.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://dom.webika.com.ua/ru/redirect?url=https://onlinecasino.us.org  http://daleyps.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://clubfabia.ru/forum/go.php?https://onlinecasino.us.org   http://www.ldshj.com/bbs/redirect.php?tid=1753&goto=onlinecasino.us.org  https://www.zakkamania.com/dir/shopsearch/ps_search.cgi?act=jump&access=1&url=https://onlinecasino.us.org   http://www.comune.orbetello.gr.it/vivere-orbetello/redirect.asp?url=https://onlinecasino.us.org  http://fuckaau.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://worldwideconsumervoices.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://designerbathkitchen.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://ieuro.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://allcoupon.jp/rd2?url=https://onlinecasino.us.org   http://troika.ifmo.ru/go.php?url=https://onlinecasino.us.org   http://nowbeyond.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://theofficialsites.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.austreale.com/membre.php?referer=https://onlinecasino.us.org  http://china-scooters.ru/go.php?https://onlinecasino.us.org  http://ryha.org/Goto.asp?url=https://onlinecasino.us.org   http://www.comro.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://fishrovex.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://gvz.vaskan.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://intext.billboard.cz/core/ad_transaction?att=4&atd=1470;8402228890041056734;7275776205120215938;1083;1115;77701;1590;3473861635477096772;200;2;6;3959256398&curl=https://onlinecasino.us.org   http://paco-mama.com/m/redirect.php?url=https://onlinecasino.us.org  http://nysdepartmentofhealth.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.askalot.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://mascarenhas.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://pawl.needloan.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://texthandler.cceg.ca/www.google.com/dp/ads?max_radlink_len=32&r=m&domain_name=fabulous.com&client=dp-voodoo21_3ph&hl=en&adtest=off&optimize_terms=on&swp=as-drid-2464369813134582&afdt=CiAKEwj0hNrfscnZAhXXJB8KHWwfBoAYBHExJ2hC3-OWKxIZAG06ipDH024CdhpNTkqRE9ocSrcpsPoc9g&uiopt=true&oe=UTF-8&ie=UTF-8&fexp=21404%2C17300002&format=s%7Cr5%7Cr5%7Cr3&adrep=0&num=0&output=afd_ads&v=3&adext=as1%2Csr1&bsl=8&u_his=6&u_tz=-300&dt=1519847661687&u_w=1440&u_h=900&biw=-12245933&bih=-12245933&isw=1440&ish=358&psw=-1&psh=-1&frm=2&uio=sl1sr1--st22sv16sa16lt35-st22sv16sa16lt35-st12sa12&jsv=13016&rurl=https://onlinecasino.us.org 
http://jiguparmar.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.raymondused.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://xmp.42line.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://205doctors.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://hotelsforlife.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://albatros-usa.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://l1idbd.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://ruen.info/redirect.php?tid=8637&goto=onlinecasino.us.org   http://hurrikini.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://kris.prgateway.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://377tyc.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://sc.jet.su/bitrix/rk.php?id=15&site_id=sc&event1=banner&event2=click&event3=2+%2F+%5B15%5D+%5Bsidebar%5D+%D0%9E%D1%84%D0%BE%D1%80%D0%BC%D0%B8%D1%82%D1%8C+%D0%B7%D0%B0%D1%8F%D0%B2%D0%BA%D1%83+%D0%BD%D0%B0+%D1%83%D1%81%D0%BB%D1%83%D0%B3%D0%B8&goto=onlinecasino.us.org   http://abcfulfillment.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://meredithdavis.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://business28.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://centurycombat.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://eastland-shoe.info/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://macmillanaudiopublishing.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://www.paulscheffert.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://app.mailingcube.nl/redirect.php?m=4d5451354e546b7a&n=4d5463344e413d3d&url=https://onlinecasino.us.org   http://www.bestmilfclips.com/cgi-bin/atx/out.cgi?link=tmx1x106x448507&u=https://onlinecasino.us.org   http://fuseanylitics.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.nlacrc.com/redirect.aspx?url=https://onlinecasino.us.org   http://mojohummer.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://ethermail.us/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://mallwayfinder.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://mcgascbillingservices.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://hyiplist.ru/goto.php?url=https://onlinecasino.us.org  http://onlinetvsguide.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://windyweb.com/cgibin/top100/out.cgi?id=spatula&url=https://onlinecasino.us.org   http://broadbandmetrophone.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://ucr.backrack.eu/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://columnists.qbling.biz/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.retirementworkshop.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://sportspartystore.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://snuggledouble.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://electrolux.ru.xx3.kz/go.php?url=https://onlinecasino.us.org  http://masslawtemps.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://www.fortrucker-env.com/leaving.aspx?ext=https://onlinecasino.us.org   http://lrf.woodconnection.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://bigbarbecue.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.askasb.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://functionaltrainingfitnesscenter.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://ccia.info/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://bonnieandhopkins.com/__media__/js/trademark.php?d=onlinecasino.us.org 
http://lightpointcapital.biz/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://www.psychevisual.com/common/redirect.html?type=lecture_advertisement&ID=23&link=https://onlinecasino.us.org   http://strosalieschool.org/__media__/js/trademark.php?d=onlinecasino.us.org   http://bayermarine.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.89zsw.com/go.asp?url=https://onlinecasino.us.org   http://ups-nextdayairsaver.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.yanbiaochina.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.traficopr.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://accfin.teiep.gr/eclass/modules/link/go.php?course=TMA217&id=108&url=https://onlinecasino.us.org 
 
 
 
 
 
https://forums.lfstats.com/viewtopic.php?f=6&t=35952
http://forum.erotyczny24.pl/showthread.php?tid=3234&pid=10474#pid10474
https://weechookeong.com/2018/05/30/masa-sekarang-menungggu-mu-part-5-tourism-msias-rm99-694-mil-speedy-gonzales-contract/
http://7olool.tk/index.php/topic,18993.new.html#new
http://sr.leet.cz/viewtopic.php?f=16&t=454