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://review-website.net/redirect.php?url=https://onlinecasino888.us.org/  http://www.anorexicnudes.net/cgi-bin/atc/out.cgi?id=17&u=https://onlinecasino888.us.org   https://www.trustinsurance.co.uk/home/security/?url=https://onlinecasino888.us.org/   http://moviemagicvpo.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://karrenbaker.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://wayside-gold.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://japanesefuck1.com/go.php?url=https://onlinecasino888.us.org  http://comixparade.com/cgi-bin/at3/out.cgi?id=115&trade=https://onlinecasino888.us.org   http://kubbafamily.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://www.desyatki.ru/redirect/index.php?url=https://onlinecasino888.us.org  http://shop4library.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://videobranson.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://logrentals.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://asmii.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://sharecycling.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://ferghana.ru/go.php?https://onlinecasino888.us.org  http://www.superstockings.com/cgi-bin/atx/out.cgi?id=66&trade=https://onlinecasino888.us.org   http://internationalschool.eduzones.com/redirect.php?url=https://onlinecasino888.us.org 
http://smdtravel.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://centralstationrealty.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://rembrandtbkk.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://sts.kzntl.gov.za/dow_internet/LinkClick.aspx?link=https://onlinecasino888.us.org  http://onlypicsfine.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://ulacrosse.org/Goto.asp?url=https://onlinecasino888.us.org  http://alephnull.net/software/go.pl?https://onlinecasino888.us.org  http://sportsmansinnovations.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.digbotz.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://27.xg4ken.com/media/redir.php?prof=2965&camp=49420&affcode=kw6070&inhurl=onlinecasino888.us.org  http://www.ship.sh/link.php?url=https://onlinecasino888.us.org   http://banner19.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://marfasvineyard.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://servingupmiseryatusf.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://oddsavisen.com.assetline.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://ultimatestinker.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://bql.garnierrewardstopfloor.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://brokersinterstate.co/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://sqz.ndtax.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.lezdomcage.com/cgi-bin/at3/out.cgi?id=21&tag=top&trade=https://onlinecasino888.us.org   http://www.tresmilranch.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://200galleries.com/go.php?url=https://onlinecasino888.us.org  http://aculoan.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://assar.kz/bitrix/redirect.php?event1=news_out&event2=http://bigwall.ru/&event3=Интернет-магазин+BigWall&goto=https://onlinecasino888.us.org  http://kaztrade.ru/bitrix/rk.php?goto=https://onlinecasino888.us.org  http://bigbangattack.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.instantwinner.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://www.finestra1.de/gewinnspiel/detect.php?VORNAME=&NACHNAME=&P_ID=0&referer=https://onlinecasino888.us.org  http://www.donnaclark.net/SearchPoint/redir.asp?reg_id=pTypes&sname=/searchpoint/search.asp&lid=0&sponsor=LAN&url=onlinecasino888.us.org   http://deviceforce.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://tecolotebaseball.org/Goto.asp?url=https://onlinecasino888.us.org  http://apinanofab.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://danielgraziano.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://mcmservices.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  https://www.garmin.ru/bitrix/rk.php?id=129&event1=banner&event2=click&event3=5+%2F+%5B129%5D+%5BGARMIN_AKCII%5D+Garmin+%E1%EE%ED%F3%F1+%ED%EE%E2%EE%F1%F2%FC+%E2+%E0%EA%F6%E8%E8&goto=onlinecasino888.us.org   http://rburgercpa.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://www.providentprofessionalgroup.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://images.google.la/url?q=https://onlinecasino888.us.org   https://medaboutme.ru/bitrix/rk.php?id=32&site_id=s1&event1=banner&event2=click&event3=1+%2F+%5B32%5D+%5Bdecode_page%5D+%D0%92%D0%B8%D0%B4%D0%B5%D0%BE%D0%BA%D0%BE%D0%BD%D1%81%D1%83%D0%BB%D1%8C%D1%82%D0%B0%D1%86%D0%B8%D1%8F&goto=https://onlinecasino888.us.org   http://johnrobertsdirect.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://encorebooks.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://coolcourt.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://abogadosdeaccidentedeconstruccion.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.clubmed-group.com.sg/cm/home2014/redirect/398/AE?url=https://onlinecasino888.us.org   http://mrlikosblogspot.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://ihatefisherinvestments.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  https://secure.parentalrights.org/nps//servlet/DisplayLink?orgId=parentalrights&emailId=69b23840b83be4d7be9aa90da04748c36m3618124169b&&linkId=7119&targeturl=https://onlinecasino888.us.org   http://immcrimatty.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://ballroomjunky.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://veteransadvocacyproject.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://gfu.sap-soft.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.discountsanddeals.com.au/go.php?url=https://onlinecasino888.us.org  http://nijeryag.tk/link?r=42920782&t=11&s=100&l=thumbs&u=https://onlinecasino888.us.org   http://torrent.janemusic.org/redir.php?url=https://onlinecasino888.us.org 
http://csharpcode.net/redir.asp?link=onlinecasino888.us.org  http://k2realty.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   https://mosremit.ru/links.php?go=https://onlinecasino888.us.org   http://wwwacademysports.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.carbootsaledirectory.co.uk/index.php?thememode=full;redirect=https://onlinecasino888.us.org   http://french4arab.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://doultonwaterpurifiers.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.nwscience.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.korfez.org/gotoURL.asp?url=https://onlinecasino888.us.org 
http://lujn.co/2048/goto.php?url=onlinecasino888.us.org  http://geeksoncall.biz/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://m.eastmarietta.com/Goto.asp?url=https://onlinecasino888.us.org   http://eventsatxcel.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://flcu.us/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://guildfamilyfoundation.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://rentodopr.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://demo.sgc-ipedito.fr/redir.php?go=https://onlinecasino888.us.org   http://crp.rebel-alliance.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
 
 
 
 
 
http://vonwolkenstein.ano-host.co.in/vb4-neu/showthread.php?416296-uriko-bloody-roar&p=570732#post570732
http://123.242.157.9/webboard_ita/index.php?topic=405430.new#new
https://forum.awarz.net/showthread.php?418140-deck-planter-box-plans&p=533170#post533170
http://installation.mannlist.com/viewtopic.php?f=10&t=45662743
http://ozelguvenlikgorevlisi.net/forum/index.php?topic=366911.new#new