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://buildadashboard.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.deepstop-dive.com/__media__/js/trademark.php?d=onlinecasinodd.com   http://469bc.co.uk/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://charlottesvillevaattorneys.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.hklawpage.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.mirroronamerica.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://menswarhouse.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://chefsdepot.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://thepleasurekit.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://flcolo.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.zscarpe.transpraia.pt/out.php?url=onlinecasinodd.com   http://strictlymetalprints.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://m.hicbc.com/i/files/acj/?url=https://onlinecasinodd.com  http://www.jasper-van-t-hof.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://graysonpartners.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  https://media.wiki-media.nl/click.php?id=10309&adurl=https://onlinecasinodd.com  http://www.mzmzj.gov.cn/page/redirect.php?url=https://onlinecasinodd.com   http://monptc.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://www.ipeoplenet.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.zhuhaionline.net/bbs//redirect.php?tid=97063&goto=onlinecasinodd.com   http://everydayhookup.acquintus-investment.de/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://gayboystwink.top/sr/out.php?l=0s.1.99.465.740&u=/onlinecasinodd.com  http://pcvbns.co/2048/goto.php?url=onlinecasinodd.com   https://fortyseven-dot-yamm-track.appspot.com/Redirect?ukey=1Vx3EDdNuxBY9ytx6t3RXzRyp_Sx5gufAL8k0SUA1d4M-2061394289&key=YAMMID-29464374&link=https://onlinecasinodd.com  http://haywintech.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://midwestwhitetailtv.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://jaylas.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://steelopolis.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://1worldhistory.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://m.shopinlasvegas.net/redirect.aspx?url=onlinecasinodd.com   http://onyourlist.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://donovanpainting.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://bondvaluationreport.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://789film.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.new.nettfront.hu/new/hu/termek-kategoriak/askquestion/form?category_id=37&product_id=13&type=0&actual_type_ask=1&tmpl=component&referer=https://onlinecasinodd.com   http://www.correctweb.de/bitrix/redirect.php?event1=referenzen_out&event2=stark-it&event3=&goto=https://onlinecasinodd.com 
http://derluftmodel.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://bestcarpetcleaneratlanta.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://rjh.dotcalmpromotions.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://alisonanderson.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.forumterzosettore.it/admanager/go.php?url=https://onlinecasinodd.com   http://iux.atlanticandgulf.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://axanet.biz/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://howard.fastcommand.com/goto.php?url=onlinecasinodd.com   http://alabamacreditunion.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://walktoendlupus.us/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://business.gov.kz/bitrix/redirect.php?event1=news_out&event2=http%3A%2F%2Fwww.pma.kz&event3=%D0%9F%D0%9C%D0%90+%D0%A2%D0%9E%D0%9E&goto=https://onlinecasinodd.com   http://lowehospitalitygroup.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.bazakabel.ru/bitrix/redirect.php?event1=catalog_out&event2=http://www.вира-групп.рф&event3=ООО+&quot;ВИРА-ГРУПП&quot;&goto=https://onlinecasinodd.com  http://everypropertylisted.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://imop.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.oldtownoil.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://noras.biz/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://jdq.mbeams.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://nautilus.inews.it/0111it/AdRedir.asp?url=https://onlinecasinodd.com/  http://www.coinsarefun.com/forums/index.php?thememode=full;redirect=https://onlinecasinodd.com   http://k2pi.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.oyeoye.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://linesell.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.btckan.com/redirect?url=https://onlinecasinodd.com  http://westcoastcannabisexchange.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.donations.com.au/redirect.asp?n=4072&u=https://onlinecasinodd.com   http://shadowglentexas.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://sauceboy.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.preston-grangecc.org.uk/goto-link.php?link_id=13&url=https://onlinecasinodd.com   http://thegardenpalette.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://edusoftware.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.runagainsthunger.org/LinkClick.aspx?link=https://onlinecasinodd.com   http://fisherinvestmentpress.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.yysf.cc/go.php?url=https://onlinecasinodd.com  http://backupacronis.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://onhiv.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://qcf.greenworld.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://hooked-on-reading.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://3pillar.eu/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://pacificstarbuck.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.cwedwardsfuneralhome.com/cfi/addthis_email.cfm?url=https://onlinecasinodd.com   https://mobile.uwants.com/redirect.php?tid=11359675&goto=onlinecasinodd.com  http://bridielogue.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://saveyourconcrete.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://hoosierbeekeeper.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://chief-security-officer.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://jerkoffer.com/cgi-bin/crtr/out.cgi?id=407&l=top&u=https://onlinecasinodd.com   http://olmoneybags.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   https://www.eastportyc.org/default.aspx?p=TrackHyperlink&url=https://onlinecasinodd.com  http://orois.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://laspas.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://newyorkhelicopterattorneys.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.nikionly.com/upskirttop/out.cgi?ses=lcckqzbUV0&id=38&url=https://onlinecasinodd.com   http://m.www.666movies.com/cgi-bin/atl/out.cgi?s=60&u=https://onlinecasinodd.com 
 
 
 
 
 
https://ovaismehboob.com/2011/09/12/linkedin-authentication-in-asp-net/
http://ru-realty.com/forum/index.php?topic=49196.new#new
https://www.knownothing.net/community2/index.php/topic,159726.new.html#new
https://csrworldnews.com/forum/index.php?topic=21204.new#new
http://dailypatriot.net/phpbb/viewtopic.php?f=4&t=50860