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.chapelhouse.co.uk/kia/send-to-friend/default.aspx?url=https://cannacbdoilrx.com  http://redpeppertube.com/streamrotator/out.php?l=gXSNSNozSKHQy&u=tp/out.php?link=17&s=1&url=cannacbdoilrx.com   http://timewarnercablecenter.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://www.ethington.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://www.bip.kielce.eu/podglad/-/asset_publisher/BAkP/content/przetarg-nieograniczony-or-iii-341-3-62-09?doAsUserId=tytmerluf&redirect=https://cannacbdoilrx.com   https://www.ustinka.kz/bitrix/rk.php?id=45&site_id=s1&event1=banner&event2=click&event3=1+%2F+%5B45%5D+%5Bindex240x400_after_articles%5D+%D0%93%D0%BB%D0%B0%D0%B2%D0%BD%D0%B0%D1%8F+240x400+%D1%81%D0%BF%D1%80%D0%B0%D0%B2%D0%B0+%D0%BE%D1%82+%D1%81%D1%82%D0%B0%D1%82%D0%B5%D0%B9&goto=cannacbdoilrx.com  http://stubby2.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://worldoftibia.ru/out.php?link=https://cannacbdoilrx.com   http://harcourtlearningdirect.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://www.xtqgzx.com/blog/go.asp?url=https://cannacbdoilrx.com  http://bayrou2017.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://c1o.qbarbecue.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://barvaskintherapy.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://jwu.jreeves-law.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://www.zaragoza-web.info/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://baisha.huizan.top/index.php?g=Appoint&c=Changecity&a=go&city=yl&referer=https://cannacbdoilrx.com  http://tutoroctavian.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://fortlewishomes.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://citadelmgmt.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://assets7.elephone.hk/static/redirect?url=https://cannacbdoilrx.com   http://www.matureland.net/cgi-bin/atx/out.cgi?id=98&trade=https://cannacbdoilrx.com   http://forum.ahigh.ru/away.htm?link=https://cannacbdoilrx.com  http://camoplaysets.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://www.quilttrips.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://spring-time.org/redirect/?url=https://cannacbdoilrx.com  http://movieawards.org/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://desmoineshomestyle.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://tpbfcu.org/redirect.php?link=https://cannacbdoilrx.com  http://gumbum.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://everypropertylisted.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://www.usacademics-ar.com/redirect.php?school=UCIEP&schoolid=80&language=ar&adsize=200x100&adtype=jpg&redirect=https://cannacbdoilrx.com  http://rate-razvlechenija-i-otdyh.dirx.ru/click.php?url=https://cannacbdoilrx.com   http://www.implantcenter.it/budapest-lokal-info/1045/go?url=https://cannacbdoilrx.com  http://www.fsc-neulingen.de/mobiquo/smartbanner/welcome.php?referer=http%3A%2F%2Fwww.fsc-neulingen.de%2Findex.php%2Fforum%2Fcredits&code=ae20b84a842ecb7d4f1eb863af7e6eae&board_url=https://cannacbdoilrx.com  http://samuelspc.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://fairwaymediallc.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://www.abpet.co.kr/opage/redirect.php?title=%EB%B9%84%EB%B0%94%ED%85%8D&url=https://cannacbdoilrx.com  http://www.allsensor.ru/forum/away.php?s=https://cannacbdoilrx.com   http://yashabab.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://code-foundry.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://www.co-concepts.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://realestatesalesgalveston.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://www.igus.bo/iProSvc/SocialBookmarks.aspx?c=US&l=en&url=https://cannacbdoilrx.com  http://prophotoborders.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://www.kutjes.ws/cgi-bin/a2/out.cgi?id=18&l=toplist&u=https://cannacbdoilrx.com 
http://foundationsoftse.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://phpvi.com/redirect.php?tid=553&goto=cannacbdoilrx.com   http://bbs.yizuren.com/redirect.php?tid=42800&goto=cannacbdoilrx.com   http://blazingtrade.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://seaportresidences.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   https://m.ozreport.com/redirect.php?https://cannacbdoilrx.com  http://varickrealty.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://www.babyssmokehousefranchise.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://cuteteenie.com/cgi-bin/out.cgi?id=133&l=top_top&req=1&t=100t&u=https://cannacbdoilrx.com 
http://newhollywoodawards.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://addaxandorxfoundation.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://wildwoodclub.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://tellaero.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://fisher-investment-guru.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://totalipcom.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://discount-pill.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://universalcoingoldbullion.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://farmsteadingchick.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://www.grodna.ru/go.php?https://cannacbdoilrx.com/  http://david-weekly.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://l4d-support.com/go?https://cannacbdoilrx.com   http://dartmoor-railway.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://stlukesmagicvalley.org/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://enlightkids.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://chempetitive.info/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://us-chinatrading.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://luw.abstractsonline.at/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://imacorporate.biz/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://fredericaresales.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://search.ringscollection.com/click.php?url=https://cannacbdoilrx.com   http://austinfoodandwinefest.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://michigandentistreferrals.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://janejoyce.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://yellowaid.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://oasisforthesoul.org/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://takeiteasyproductions.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://www.arrivestaffing.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://nutriforme.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://stpetechildrens.org/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://myrubicon.ru/go.php?url=https://cannacbdoilrx.com  https://wssupport.ru/bitrix/rk.php?goto=https://cannacbdoilrx.com   http://en.e-hotel-pattaya.com/common/link.php?a=https://cannacbdoilrx.com  http://www.lovelymauihomes.com/index.php?setmonths=360&setpayment=0.8&setrate=0.05&jaxval=&goto=cannacbdoilrx.com  http://madna.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://afonsoferreira.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
 
 
 
 
 
https://forum.sofort.support/index.php/topic,61556.new.html#new
https://alvoria.pl/forum/index.php?topic=10664.new#new
http://forum.rodeal.ru/viewtopic.php?f=7&t=149609
http://www.rmlalliance.com/forum/index.php?topic=19781.new#new
http://conceptsnclarity.com/forum/index.php?topic=116076.new#new