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.terqp.com/bbs_cnool_net/login.aspx?referer=https://onlinecasino.us.org  http://www.telesiscde.biz/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://1550254.r.bat.bing.com/?ld=d3asSzdhyqnFdB3Fqg1YCjtzVUCUyuilIY8vGm2DyglfedHFzI72mOIFxjA5fw50n1LORltxIZug2ymxP2EKl3-9IkmM_lITv-Wcmb9KX_wS3X7DsPBvlG2Mi-I-tW6BLBsFOZopyzioSlk4VBsy-w1MWF8fWltvtiW5HA69qy8tiw2_0r&u=https://onlinecasino.us.org   http://telusmobilityblows.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.adventuresoutdoors.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://stagefoundation.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://antmancostumes.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://unionbancaireprivee.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://grounding-lightning.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://nextregistrationrights.us/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://shoppermarketingsummit.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://stephennosal.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.mearsashbygroup.org.uk/goto-link.php?link_id=36&url=https://onlinecasino.us.org  http://www.hizliizlefilm.net/r/728.html?reklam=1457827200&referer=https://onlinecasino.us.org   http://wefixbikes.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://mopon.ru/out.php?link=https://onlinecasino.us.org  http://powerboatgear.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://bondgo.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://0nm.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://ghobashinvestments.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://tarbellrealtorsonline.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.selectivemutismfoundation.org/guestbook/go.php?url=https://onlinecasino.us.org  http://www.realcanadiansuperstore.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://forplaymarket.com/go.php??s=https://onlinecasino.us.org  http://myykkbrz.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://greatestits.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://yourfinaldirective.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://pynnfamily.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.fujiki-naohito.com/redirect.php?goto=onlinecasino.us.org   http://oleis.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.creativepastimes.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://da.urbanrail.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://digitizationarchitects.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://jackdraymond.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.fattysexvids.com/cgi-bin/atx/out.cgi?id=28&trade=https://onlinecasino.us.org   http://www.cotedor-tourisme.com/objets/redirect.php?url=https://onlinecasino.us.org 
http://fiestamaids.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://mrbanner.3dn.ru/go?https://onlinecasino.us.org   http://www.fatretirement.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://voicecommerce.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://depuoz.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://workoutforhope.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://se.tom.ru/engine/redirect.php?url=https://onlinecasino.us.org  http://mollybrownsummerhouse.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://dujiao.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://panerapantry.info/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.apple-gallery.net/gallery/main.php?g2_view=core.UserAdmin&g2_subView=core.UserLogin&g2_return=https://onlinecasino.us.org   https://webmail.math.unifi.it/horde-webmail-1.0/services/go.php?url=https://onlinecasino.us.org   http://www.mustseetoday.co.uk/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://northwestchicagorealestate.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://youramazingpictures.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.bustydaily.com/cgi-bin/atx/out.cgi?id=66&trade=https://onlinecasino.us.org  http://www.erodocdb.dk/doks/filedownload.aspx?fileid=3740&fileurl=https://onlinecasino.us.org   http://thespringshealthcenter.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://www.bg-auto.com/go.php??s=https://onlinecasino.us.org  http://slchospital.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://musikon.se/exit.php?url=https://onlinecasino.us.org   https://www.undergroundnews.com/link.php?system=redirect&url=https://onlinecasino.us.org  http://eastmarkawesomefest.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://olginfo.info/redirect.php?tid=2261&goto=onlinecasino.us.org  http://ndhockey.com/Goto.asp?url=https://onlinecasino.us.org  http://www.fresherswalkins.in/Home/Redirect?url=https://onlinecasino.us.org   https://www.diagraph.com/LinkClick.aspx?link=https://onlinecasino.us.org 
http://ilovemills.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://quiznosux.us/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.antiquar.pro/go.php?go=https://onlinecasino.us.org   http://jdmcapitaldesigns.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://soccertryouts.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://itstaxservice.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://breakingideas.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://aybsa.com/Goto.asp?url=https://onlinecasino.us.org   http://ladyboy8.com/cgi-bin/a2/out.cgi?id=24&u=https://onlinecasino.us.org 
http://www.champtruck.us.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://namastetv.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.australiatodaymag.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://glhecsux.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://unspunsports.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.cui.edu/LinkClick.aspx?link=https://onlinecasino.us.org  http://autopartescalifornia.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://carysma.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://wildenpartskit.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://idsunsystem.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://ostrichplace.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://sencor.us.xx3.kz/go.php?url=https://onlinecasino.us.org   http://www.weiberecke.de/wbb2/thread.php?goto=onlinecasino.us.org  http://contemporaryclothes.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://choosehim.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.taodaxiang.cn/p.php?go=https://onlinecasino.us.org  https://www.fnol.cz/redirect.asp?link=https://onlinecasino.us.org   http://quepasacuba.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
 
 
 
 
 
https://jerry-coleby-williams.net/2018/04/26/market-gardening-vietnam/
http://gwforum.com.br/viewtopic.php?f=18&t=271694
https://truthinreality.com/2015/09/15/prayer-for-breaking-the-powers-of-witchcraft/
http://niklever.com/spritoon/forum/viewtopic.php?f=5&t=317936
https://www.dangeroussports.com/forum/viewtopic.php?pid=473945#p473945