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.takeshikaneshiro.net/forum/redirect.php?fid=37&tid=2001&goto=americacbdoilstore.com  http://dentistinsanmateo.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://loseyourself.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://citc-92.citci.clipe.pt/cgi-bin/atc/out.cgi?id=95&u=/americacbdoilstore.com  https://deairank.mistypark.com/01/out.cgi?id=urashi10&url=https://americacbdoilstore.com   http://jewelsearchengine.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://lovebablo.info/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://engelsfee.ch/cms/?redirect&url=https://americacbdoilstore.com   http://portsolutions.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com 
http://www.eurocorp-international.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://avk-opt.ru/bitrix/rk.php?id=5&site_id=s1&event1=banner&event2=click&event3=1+%2F+%5B5%5D+%5BBAN1%5D+%CF%F0%E0%E2%FB%E9+%E1%E0%ED%ED%E5%F0+1&goto=https://americacbdoilstore.com   http://gi7.bk-entertainment.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://halodryie.gq/go.php?track=chABNnlFKmlCdUQGeg==&url=americacbdoilstore.com  http://xn--b1aajkzh1ewd.xn--p1ai/go.php?to=https://americacbdoilstore.com   http://ottertail-power.info/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.lakeplacidwaterfrontestate.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://summittrustee.us/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://reservati.zujihotel.com.tw/redirect.php?fid=4&tid=127633&goto=americacbdoilstore.com 
http://eggtraymachine.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  https://csharp.love/go.php?url=https://americacbdoilstore.com   http://renzervhcp.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://pennyprimer.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.aladura.org/__media__/js/trademark.php?d=americacbdoilstore.com   http://franchisedirect.tv/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://tokyobowling.info/redirect.php?tid=3905&goto=americacbdoilstore.com  http://www.magicvivaldi.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://www.bikini-swimwear.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com 
http://cdn2.milf-porn.sexy/dcpn/cgi/out.php?url=americacbdoilstore.com  http://asfma.info/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://31.172.83.162/forum/big-fat-team-announcements/253-share-our-banner?goto=americacbdoilstore.com   http://ww.coo123.net/goto.php?c=&n=ID&url=https://americacbdoilstore.com  http://www.dt.co.kr/contents.html?article_no=2018021302101231037002&ref=naver232386&q=%B0%FA%B1%E2%C1%A4%C5%EB%BA%CE&ssc=tab.news.all&f=news&w=news&s=eGpxiECBISzkAdpR0K8HCQ==&time=1518474755689&a=nws*y.outtit&r=22&i=880000AD_000000000000000002448365&g=029.0002448365&u=https://americacbdoilstore.com   http://www.mercyworld.org/e-news/redir.cfm?articleid=69AE6C14-BD5E-075C-FE1EE55B75D44079&url=https://americacbdoilstore.com  http://katewhitney.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://moneymastersdirect.biz/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   https://mova.org.ua/out.php?link=https://americacbdoilstore.com 
http://asr.pokedex.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  https://www.ghareng.com/go.php?to=americacbdoilstore.com   http://photog.hk/redirect.php?fid=20&tid=2927&goto=americacbdoilstore.com   http://dutchmans.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.stimcointernational.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://novelasportuguesas.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.gomaturesex.com/cgi-bin/a2/out.cgi?id=114&l=top10&u=https://americacbdoilstore.com  http://visionstop.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://fincalab.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com 
http://dnabiotek.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://jeu.jeffersonhealthsystem.org/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://cherrycreekmanagement.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://www.heartdrippings.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://nrj.capturemyinfo.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://eustisbenefits.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://njalternativedisputeresolution.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://club-edu.tambov.ru/main/search/club.cgi?cc=1&q=%E3&URL=http%3A//americacbdoilstore.com/&wm=wrd   http://reorentals.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com 
http://calpinewindfarms.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://zelnikrealestate.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://richardsonstudentsample.wikispaces.com/-/Dos%20Lados%20Politicos/Team%203/page/edit/American+politician?goto=https://americacbdoilstore.com   http://www.fudgeismylife.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.vasaricountryclub.com/default.aspx?p=TrackHyperlink&url=https://americacbdoilstore.com   http://overturecenterfoundation.info/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.allmonitors.co/goto.php?url=https://americacbdoilstore.com  http://365lenovo.com/url?q=https://americacbdoilstore.com   http://valk.kinderdijk.vakantieland.nl/klikuit.asp?id=-514216090&url=https://americacbdoilstore.com 
http://www.pruducts.review/go/eafengrow-zt-0999-%D1%81%D0%BA%D0%BB%D0%B0%D0%B4%D0%BD%D0%BE%D0%B9-%D0%BD%D0%BE%D0%B6%D0%B8-zt0999-d2-%D1%81%D1%82%D0%B0%D0%BB%D1%8C-%D1%83%D0%B3%D0%BB%D0%B5%D1%80%D0%BE%D0%B4%D0%BD%D0%BE%D0%B3%D0%BE-%D0%B2%D0%BE%D0%BB%D0%BE%D0%BA%D0%BD%D0%BE-%D1%87%D0%BF%D1%83-%D0%BB%D0%B5%D0%B7%D0%B2%D0%B8%D0%B5-%D0%BA-658716?type=4&sourceurl=https://americacbdoilstore.com  http://www.stagnarobros.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   https://match.presdo.com/redirect?conference_id=sway-minds&track=signup&url=https://americacbdoilstore.com   http://mrpolicy.trf.or.th/LinkClick.aspx?link=https://americacbdoilstore.com  http://lwm.shola.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://www.ethanmackjackson.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://twtc.info/__media__/js/netsoltrademark.php?d=americacbdoilstore.com  http://www.acvnet.org/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://www.realestateinocmd.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com 
http://www.7bed.com/home/link.php?url=https://americacbdoilstore.com  http://nickperricone.net/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://vintagesonomasenior.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://www.ituiba.cn/wp-content/themes/begin4.2/inc/go.php?url=https://americacbdoilstore.com  http://hitechtoybox.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://www.chicagogop.com/r?u=https://americacbdoilstore.com  https://book-ye.com.ua/bitrix/rk.php?id=32&site_id=s1&event1=banner&event2=click&event3=1+%2F+%5B32%5D+%5BTOP%5D+Small+%D0%A1%D0%B8%D0%BB%D0%B0+%D0%B2%D0%BE%D0%BB%D1%96&goto=americacbdoilstore.com  http://www.mok007.net/wordpress/wp-content/themes/begin/inc/go.php?url=https://americacbdoilstore.com   http://armorfloorepoxy.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com 
https://www.bergfex.at/to/?url=https://americacbdoilstore.com  http://folar.nationbuilder.com/r?u=https://americacbdoilstore.com   http://www.jewishinternetguide.com/main/mod.php?mod=weblink&op=visit_link&lid=2124&url=https://americacbdoilstore.com   http://0directory.com/togo.php/americacbdoilstore.com  http://pchmedia.org/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://www.xa999.com/hi/link.php?url=https://americacbdoilstore.com  https://action.pipsc.ca/r?u=https://americacbdoilstore.com  http://ymk.susanreinstein.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com   http://ashleyleasure.com/__media__/js/netsoltrademark.php?d=americacbdoilstore.com 
 
 
 
 
 
http://km-schools.edu.sa/vb/showthread.php?444-viagra-used-by-women-Nege&p=337316&posted=1#post337316
http://www.forum-hacker.com.br/showthread.php?69653-Used-berettashotguns-for-sale&p=141372#post141372
http://ghanasdgs.info/2016/04/20/these-7-things-will-change-the-way-you-approach-travel/#comment-38055
http://cheerdown.com/forum/showthread.php?tid=32605
http://www.jjhservers.com/phpbb/viewtopic.php?f=6&t=2316&p=9115#p9115