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.drisag.ch/php/mhs.printme/index.php?url=https://onlinecasino888.us.org  http://pcpcsingles.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://swepub.kb.se/setattribute;jsessionid=25CD38870521E84BD240573253DFEA75?language=en&redirect=https://onlinecasino888.us.org   http://simpleplan.su/go?https://onlinecasino888.us.org  https://partner.stiebel-eltron.cz/skoleni/qc_extlink.php?go=https://onlinecasino888.us.org   http://www.donerewards.biz/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  https://www.stadt-gladbeck.de/ExternerLink.asp?ziel=https://onlinecasino888.us.org  http://www.nikki4oakland.com/r?u=https://onlinecasino888.us.org   http://mylanpharms.in/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://anguillapages.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.hivviralload.com/process/RedirectN?url=https://onlinecasino888.us.org   http://taxcare.co/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   https://intercity.by/bitrix/rk.php?id=664&site_id=s1&event1=banner&event2=click&event3=1+%2F+%5B664%5D+%5Bmain_tours%5D+%D0%9A%D0%98%D0%9F%D0%A0&goto=onlinecasino888.us.org  http://business-international.eu/url?q=https://onlinecasino888.us.org   http://www.paineproperties.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.ladyboysurprises.com/cgi-bin/at3/out.cgi?id=160&tag=top&trade=https://onlinecasino888.us.org  http://biotracer.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.javaroad.jp/news/redirect.jsp?link=https://onlinecasino888.us.org 
http://kaesernblair.biz/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://aveacalisiyor.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://napieralski.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://jx.yuhuzm.top/index.php?url=https://onlinecasino888.us.org  http://inpoweryourpets.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://wellnessprompts.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://counterrevolutionstudio.com/LinkClick.aspx?link=https://onlinecasino888.us.org  http://app.semi.org.cn/ads/adsclick.action?aid=4d7a6379&redirect=https://onlinecasino888.us.org   http://punkmailer.de/egroupware/redirect.php?go=https://onlinecasino888.us.org 
https://www.zoopet.com/forum/blogs/maria-sj%C3%B6jungfru/2308950-?goto=onlinecasino888.us.org  http://goldmate.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://thankyoufordying.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://hairyflicks.com/cgi-bin/atc/out.cgi?id=34&u=https://onlinecasino888.us.org  http://lancerlacrosse.org/Goto.asp?url=https://onlinecasino888.us.org   http://info.lawkorea.com/asp/_frame/index.asp?url=https://onlinecasino888.us.org  http://pilotyourpower.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.lieqiwang.com/link.php?action=previous&fid=521&tid=222013&fpage=0&goto=onlinecasino888.us.org   http://www.the-spades-guide.com/go.php?passing=onlinecasino888.us.org 
https://www.graubuendner.ch/goto.php?url=https://onlinecasino888.us.org  http://intellirater.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.trophies.de/forum/index.php?app=core&module=system&controller=redirect&url=https://onlinecasino888.us.org   http://www.myonlinestats.com/traffic/go/go.php?url=https://onlinecasino888.us.org  http://www.thomashleefunds.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://baldi.ge/bitrix/rk.php?id=74&event1=banner&event2=click&event3=5+%2F+%5B74%5D+%5BMAIN_SHOE_FINDER%5D+Finder+geo&goto=onlinecasino888.us.org  http://bq--3b63fdppm34fyqa.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://metallord.org.ua/go.php?url=onlinecasino888.us.org   http://www.xn--122-5cda6bgxq.xn--p1ai/bitrix/rk.php?id=10&event1=banner&event2=click&event3=1+%2F+%5B10%5D+%5Bd_nav%5D+%CA%E0%EB+%ED%E0+%F1%EA%F0%FB%F2%F3%FE+%EA%F0%EE%E2%FC+%28Hb%2FHp+%F2%E5%F1%F2%29&goto=onlinecasino888.us.org 
http://hotelcrowdfund.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://shemale-3d.com/crtr/cgi/out.cgi?id=89&l=top_top&u=https://onlinecasino888.us.org   https://xn--90athc1g.xn--p1ai/?name=camera1&ip=185.58.252.84&type=jpeg&request_type=new_session&duration=0&stream_clients=0&total_clients=0&session_id=9fca65450912bab2f973a7f22c391af960eb6b61&country=RU&user_name=camera1&port=80&qs=&host=185.197.33.237&user_agent=Mozilla%252F5.0+%2528Windows+NT+6.1%253B+Win64%253B+x64%2529+AppleWebKit%252F537.36+%2528KHTML%252C+like+Gecko%2529+Chrome%252F61.0.3163.100+Safari%252F537.36&referer=https://onlinecasino888.us.org   http://jump.spawn.jp/?url=https://onlinecasino888.us.org  http://managingwithinformation.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   https://www.bnaibrith.ca/r?u=https://onlinecasino888.us.org  http://winstonli.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  https://maestro.technolutions.net/partner/remote?u=https://onlinecasino888.us.org   http://bbs2.dl.net.cn/uchome/link.php?url=https://onlinecasino888.us.org 
http://redcoproperties.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://toddcory.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://jikosha.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.tsticn.com/redirect.aspx?url=https://onlinecasino888.us.org  http://images.google.mn/url?q=https://onlinecasino888.us.org   http://streamlinedaccounting.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://menopauseformale.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://the-outnet.us/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.fullboost.com.au/forum/mobiquo/smartbanner/ads.php?referer=http%3A%2F%2Fwww.fullboost.com.au%2Fforum%2Fshowthread.php%3F8840-FS-SA-Garrett-TO4-S-1050-delivered&code=383c7f0a1d2d0bb10f9695b658ba1800&board_url=https://onlinecasino888.us.org 
http://eatingdisordersadolescent.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://rpokhotsk.ru/bitrix/rk.php?event1=file&event2=download&goto=onlinecasino888.us.org   http://fusebus.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://dealofthedayindia.com/go.php?https://onlinecasino888.us.org  http://www.educateourstate.org/r?u=https://onlinecasino888.us.org   https://matbrod.ifokus.se/links/track?type=featured&url=https://onlinecasino888.us.org  http://www.chickenhead.com/includes/redirect.asp?url=https://onlinecasino888.us.org  http://fructositis.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://revisaodosite.com/redirect.php?url=https://onlinecasino888.us.org 
http://gzg.academyofartfacultysucks.biz/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.economads.com/link.php?https://onlinecasino888.us.org   http://ultimatehondaexperience.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://romhacking.net.ru/go?https://onlinecasino888.us.org  http://www.biochemistry.org/LinkClick.aspx?link=https://onlinecasino888.us.org   http://efolks.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://gloryonhighfireworksllc.com/__media__/js/trademark.php?d=onlinecasino888.us.org  http://taitzfamily.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://unityofeffort.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://www.transpacificdistributors.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://mrbanner.3dn.ru/go?https://onlinecasino888.us.org   http://hotgayflicks.com/go.php?s=60&u=https://onlinecasino888.us.org   http://www.newfashionmod.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.golfclubambrosiano.it/gotoURL.asp?url=https://onlinecasino888.us.org   https://www.bitrix24.kz/bitrix/rk.php?id=85&site_id=kz&event1=banner&event2=click&event3=1+%2F+%5B85%5D+%5BMAIN_PAGE_VIDEO%5D+%D0%91%D0%B0%D0%BD%D0%BD%D0%B5%D1%80+%D0%BD%D0%B0+%D0%B3%D0%BB%D0%B0%D0%B2%D0%BD%D0%BE%D0%B9+%28%D0%BD%D0%BE%D0%B2%D0%BE%D0%B5+%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE+KZ%29&goto=https://onlinecasino888.us.org  http://finalls.ru/forum/away.php?s=https://onlinecasino888.us.org  http://costaricasocceracademy.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.fsas.org/LinkClick.aspx?link=https://onlinecasino888.us.org 
 
 
 
 
 
https://bolasepakmy.com/mybb/showthread.php?tid=18864
http://detayweb.com/showthread.php?4408-cute-volleyball-team-names&p=4421#post4421
http://forumgeos.tk/viewtopic.php?f=4&t=48341
http://dnpel.ru/for/index.php?topic=15560.new#new
http://biko-pc.pl/forum/showthread.php?tid=23034