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://eldredfamily.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://1236.xg4ken.com/media/redir.php?prof=607&affcode=uc%7CSpredfast_GooglePlus%7CRetailMeNot%7C10/17/14%20Blog%20G%2B%20Worst%20Halloween%20Costumes%20Ever%7C32512479%7Cbroad&url=https://americacbdoilstore.com   http://search.wi.gov/cpp/cs.html?url=https://americacbdoilstore.com   http://ceh.whoswining.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.ecards100.com/cgi-bin/rank/out.cgi?id=humorpag&url=https://americacbdoilstore.com   https://tget.me/c?p=fFBPUFVMQVJfQ1RSfFNJREVCQVJ8fHBsYW5ldA==&r=https://americacbdoilstore.com  http://nutrition-charts.org/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.hotelsiesta.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://iinet2.org/__media__/js/netsoltrademark.php?d=americacbdoilstore.com 
http://caradecu.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://ngtrewards.mobi/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   https://www.gaytorrent.ru/redir.php?url=https://americacbdoilstore.com   http://www.tsticn.com/redirect.aspx?url=https://americacbdoilstore.com  http://www.legendscc.com/default.aspx?p=TrackHyperlink&url=https://americacbdoilstore.com   http://vaportight.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.labourworks.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://lolayoung.com/out.php?https://americacbdoilstore.com   http://sr.kafito.com/login?referer=https://americacbdoilstore.com 
http://www.andersonschoel.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://xporntube.xxx/go.php?track=dhtMf3tCKmtDe0cBew==&url=https://americacbdoilstore.com   https://sanisilesia.pl/redirect.php?action=url&goto=https://americacbdoilstore.com   http://www.5dcad.cn/bbs./redirect.php?tid=253389&goto=americacbdoilstore.com  http://www.microsoftproject15.ru/redir.asp?id=https://americacbdoilstore.com   http://images.google.so/url?q=https://americacbdoilstore.com  http://www.inout.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.trade-shows.eu/xternal_link.php?goto=https://americacbdoilstore.com   http://www.bjerhu.com/dialog_1.htm?arg=9005750&style=1&kf=&kflist=on&kf=296199144%40qq.com%2C583913611%40qq.com&zdkf_type=1&language=zh-cn&charset=gbk&username=&userinfo=&introurl=&lyurl=&lytype=0&copartner=&referer=https://americacbdoilstore.com 
http://fc-metalist.com/go.php?go=https://americacbdoilstore.com  http://mysoltera.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://outpick.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://www.taiphanmemdt.com/r.php?u=https://americacbdoilstore.com  http://mylanmsadvocate.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://forums.hdtvtest.co.uk/index.php?thememode=full;redirect=https://americacbdoilstore.com  http://willtothink.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.exactshot.com/redir.php?url=https://americacbdoilstore.com   http://crumandforsterindemnity.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com 
http://images.google.com.ng/url?q=https://americacbdoilstore.com  http://www.doityourselfdui.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://649lotto.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://go.rhr.fm/?to=https://americacbdoilstore.com  http://wellnessprompts.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://poddabie.sailing88.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://deptnav.smfnew2.com/index.php?thememode=full;redirect=https://americacbdoilstore.com  http://eaglesmusic.eu/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://perm.kinderra.ru/redirect?url=https://americacbdoilstore.com 
http://bbs.chetxia.com/ri.aspx?url=https://americacbdoilstore.com  http://vdyyoutube.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   https://www.abw.by/go.php?url=americacbdoilstore.com   http://biggsb.co/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.stayonvacation.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://vmsinsight.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://visualdynamics.email/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://catdrink.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://www.movimentovivabrasil.com.br/redir.php?mace2_cod=60&pess2_cod=1010&lenc2_cod=&url=https://americacbdoilstore.com 
http://www.churnvalley.org.uk/goto-link.php?link_id=17&url=https://americacbdoilstore.com  http://ohn.urbanaddress.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://nycskyliners.assn.la/Goto.asp?url=https://americacbdoilstore.com   http://mylan-moves.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.inkalinux.com/foros/mobiquo/smartbanner/ads.php?referer=http%3A%2F%2Fwww.inkalinux.com%2Fforos%2Fmember.php%3F74-redyser%26tab%3Dactivitystream%26type%3Dfriends%26page%3D1&code=d41d8cd98f00b204e9800998ecf8427e&board_url=https://americacbdoilstore.com   http://www.lipstickpanda.com/s/go/https://americacbdoilstore.com  http://dimelectrico.com/skin/frontend/default/french/images/cache/index.php?q=https://americacbdoilstore.com  http://vidrokr.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://tahomalax.com/Goto.asp?url=https://americacbdoilstore.com 
http://yc.xinshizaixian.com/index.php?g=Appoint&c=Changecity&a=go&city=xsbn&referer=https://americacbdoilstore.com  http://mannygarciacompanies.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://ax.0595rc.com/user/url.php?url=https://americacbdoilstore.com   http://www.stgoranandstmichael.org.uk/goto-link.php?link_id=12&url=https://americacbdoilstore.com  https://www.okzk.lv/rogaining_lv/forum/go.php?url=https://americacbdoilstore.com   http://marketbhavishya.co.in/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://medicineinfo.awardspace.com/go.php?url=https://americacbdoilstore.com  http://crosscore-uk.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://vanauken.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com 
http://websetters.acquaverde.de/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://qhp.argosytours.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://infoaccount.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://stewartprivateestates.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://voteme100.com/cgi-bin/toplist/out.cgi?id=ourwives&url=https://americacbdoilstore.com   http://myfd.ru/redirect?url=https://americacbdoilstore.com  http://www.myweedseeds.com/affiliate.php?id=267&redirect=https://americacbdoilstore.com  http://pioneermilitaryloan.us/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://wrimdocs.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com 
http://keenansafehospitals.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  https://www.lovearmy.org/r?u=https://americacbdoilstore.com   http://winneteurope.dmsbike.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://bangladesh-investment.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://onlinecampaigncenterrs.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://xw.membercenter.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  https://www.voyazteca.com/vb/redirector.php?url=https://americacbdoilstore.com  http://insidevail.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://incityoffers.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com 
 
 
 
 
 
http://mastazone.nl/viewtopic.php?f=19&t=8879
https://blackdiamond-casino.com/forum/viewtopic.php?pid=283221#p283221
https://www.lcb-informatique.ch/forum/viewtopic.php?pid=98340
http://forum.sonturkrpg.org/showthread.php?tid=42603&pid=54478#pid54478
http://dailypatriot.net/phpbb/viewtopic.php?f=4&t=22401