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://go.cmsystem.ir/index.php?url=https://onlinecasino888.us.org  http://issuesandwhatnot.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.kupirebenku.ru/bitrix/rk.php?id=119&event1=banner&event2=click&event3=1+%2F+%5B119%5D+%5Bmain_carusel%5D+%CB%E5%E4%E8+%C1%E0%E3+&goto=https://onlinecasino888.us.org   https://www.gopoyou.com/ajax/click.php?url=https://onlinecasino888.us.org  https://sbm.springbranchisd.com/LinkClick.aspx?link=https://onlinecasino888.us.org   http://www.konjf.cf/cgi-bin/a2/out.cgi?id=300&l=toplist&u=https://onlinecasino888.us.org  http://atlantisrsps.smfnew2.com/index.php?thememode=full;redirect=https://onlinecasino888.us.org  http://netgearprosafe.biz/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://sitrickandcompanyinc.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://eslgarage.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://gracepayment.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://geochemresearch.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://yodaweb.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.federcomated.it/redirect.asp?link=https://onlinecasino888.us.org   http://norcoautomotive.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://towerhealth.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://desclock.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://eceaa.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://ginlime.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://amarishomes.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://circleoffaith.biz/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://mqj.shellusa.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://sonofdave.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://autohgregorythecarsuperstore.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://nyhealth.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://banery.media.com.pl/goto-125.php?url=https://onlinecasino888.us.org   http://ushispanicfund.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://www.rustrahovka.ru/bitrix/rk.php?goto=https://onlinecasino888.us.org  http://jasongriffin.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://forum.noamweb.com/viewthread.php?goto=onlinecasino888.us.org   http://proteined.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://dev.index63.ru/redirect.php?redirect=https://onlinecasino888.us.org   http://www.polywater.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://333322.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://questforvictory.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   https://www.lacasting.com/frontend/ADVERTISING/ASP/adclick.asp?key=1068&url=https://onlinecasino888.us.org/ 
http://brucekayebraintumorfoundation.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://tulahfuchs.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://missysteve.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.facultyadvisers.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  https://www.poetrysoup.com/redirect.aspx?ID=23&url=https://onlinecasino888.us.org   http://rbv.nonstop-webs.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://nashvilleitjobs.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.hespress.com/opinions/index.137.html?zoneid=6&amp;cb=15981353144&amp;charset=UTF-8&amp;loc=http%3A/www.hespress.com/opinions/index.137.html&amp;referer=https://onlinecasino888.us.org   http://gaschoolshop.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://kirklandarts.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://blood-bowl.nl/postlist.php?eol_ref=https://onlinecasino888.us.org/   http://officialtravel.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://fdamustbanmenthol.tv/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://axadistributors.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://carmelvalleyinfo.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://wow.mine.sk/chat/redir.php?url=https://onlinecasino888.us.org  http://tv-sat.at.ua/go?https://onlinecasino888.us.org   http://podnajmy.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://ubaafricard.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://103.35.173.171/bbs/redirect.php?fid=143&tid=3942875&goto=onlinecasino888.us.org   http://www.tokyo-underground.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.pondofhappiness.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://mypinchin.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://fejda.se/index.php?q=https://onlinecasino888.us.org  http://immerseyourself.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://sfgov3.org/redirect.aspx?url=https://onlinecasinolime..com   http://nsz.urbandermatology.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://ccmpcapital.tc/__media__/js/trademark.php?d=onlinecasino888.us.org  http://www.roundstrading.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://moneypal.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.bebesymas.com/redirect?url=https://onlinecasino888.us.org  http://froggg.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://ovg.7billion.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  https://fusne.com/go.php?link=https://onlinecasino888.us.org  http://www.irisinspection.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://crumandfosterinsurance.us/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://backrack.eu/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  https://www.loopme.ph/redirect/c?url=https://onlinecasino888.us.org   http://blackboardintl.biz/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://anti-lineage2.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.otaracatholic.org.nz/LinkClick.aspx?link=https://onlinecasino888.us.org   http://100wiz.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://tubeporr.org/ept/out.php?f=1&pct=40&url=https://onlinecasino888.us.org  http://montanterealestate.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://applebeesmysteryvalue.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://veryvy.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://northtexturb.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.intellika.biz/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://jennifersultan.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://herzogmotorsports.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://stereology.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.aisouhao.com/url.php?url=https://onlinecasino888.us.org  http://uminewton.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   https://www.cbmmortgage.com/modules/mod_jw_srfr/redir.php?url=https://onlinecasino888.us.org 
 
 
 
 
 
https://jerry-coleby-williams.net/2018/04/26/market-gardening-vietnam/
http://school9m.org.ru/node/1#comment-99344
https://ovaismehboob.com/2011/09/12/linkedin-authentication-in-asp-net/
http://www.dexteracademy.in/blog/dexteracademy/military-engineer-services-recruitment-2015-2658-mate-cmd-peon-other-posts/#comment-8119
http://www.holandia.igns.pl/forum/showthread.php?tid=4663