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://divorce-solutions.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://w.profishintl.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://creditunionprotectionplans.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.thehungerartists.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.petbirdreport.com/resources/addYourLink.asp?site=onlinecasino.us.org   http://edinayasemya.ru/away.php?url=https://onlinecasino.us.org  http://nationalgeographickidsturkiye.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://thepecasogroup.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://instituteforgood.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://ap360solutions.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://yqi.gpls-survey.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://8merrill.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://elamanecerdelmilenio.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.ass4all.com/out.php?l=https://onlinecasino.us.org   http://www.praveorechove.com/redirect.php?https://onlinecasino.us.org  http://www.jaeahn.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://departamento.metroscubicos.com/noindex/quotation/MLM613701805?go=https://onlinecasino.us.org   http://mail.sealmag.com/cgi-bin/go?https://onlinecasino.us.org 
http://rodonews.com/out.php?url=https://onlinecasino.us.org/  http://www.elcampellochristiancommunity.org/goto-link.php?link_id=18&url=https://onlinecasino.us.org   http://pagomisviajes.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://essentialtruths.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.isfun.nl/cgi-bin/isfun/gratis/out.cgi?id=promotie&url=https://onlinecasino.us.org   http://explorethenorthshore.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://printsymphony.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://ficofortress.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://realprayingwives.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://www.detiseti.ru/redirect.php?u=https://onlinecasino.us.org  https://www.elliottwave.com/r.asp?acn=4CT&rcn=aa431&url=https://onlinecasino.us.org   http://azamira.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://authentisign.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://xn--l3cf5dg3btj9i.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://zogwatch.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://fogcitymagazine.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://thelimestonegrill.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://kirov.xzaa.ru/go.php?id=33780&url=https://onlinecasino.us.org 
http://gvp.ragemotorsports.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://aafpusa.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://orbanbiotech.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://kessingerentertainment.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://5stepanimalwelfarerating.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://sajamedia.com/v2/url?u=https://onlinecasino.us.org  http://flyingkingranch.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://hawaiihostels.info/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://batteryclinic.info/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://iko.misterhide.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://erikanddanielle.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://lighthouseharbour.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://adekb.ru/upload/rk/029/029de0f574b5e7c1465960b62326fc91.swf?flash_link=onlinecasino.us.org  http://www.sexhairytgp.com/cgi-bin/at3/out.cgi?id=82&tag=toplist&trade=https://onlinecasino.us.org   http://analtorture.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.gloryholeswallowing.com/cgi-bin/autorank/out.cgi?id=mk0199&url=https://onlinecasino.us.org  http://naresav.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://nightforceproshop.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://b4u.lawandordercriminalintent.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://texasshooting.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.nbaersplayground.com/cgi-bin/redirect.cgi?url=https://onlinecasino.us.org   http://www.imaoshan.com/home/link.php?url=https://onlinecasino.us.org  http://www.cheapdealuk.lesgh.com/go.php?url=onlinecasino.us.org   http://briefnovel.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://freerobux.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://collegeradionews.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://adult.misty.ne.jp/rank/old/out.cgi?id=heart&url=https://onlinecasino.us.org 
http://burca.biz/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://courtneydesena.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.dexter-vom-jordanbach.de/gallery/main.php?g2_view=core.UserAdmin&g2_subView=core.UserRecoverPassword&g2_return=https://onlinecasino.us.org   https://www.skfintech.cn/wp-content/themes/begin/inc/go.php?url=https://onlinecasino.us.org  http://www.tzjulang.com/goto.asp?url=https://onlinecasino.us.org   http://alanjaylerner.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://inflateaflix.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://beerplace.com.ua/r.php?url=https://onlinecasino.us.org   http://hrlawgroup.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://www.cuntsarea.com/crtr/cgi/out.cgi?id=84&tag=v&trade=https://onlinecasino.us.org  http://s.anicausa.de/htref.php?u=https://onlinecasino.us.org   http://discsupersite.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://capturelivehd.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://fuegova.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://webbraces.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://prepareware.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://microexchangeoutlet.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://gruponear.com.mx/index.php/noticias2/item/32-neuromarketing?fromurl=onlinecasino.us.org 
http://etraffel.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://www.sbafla.com/method/LinkClick.aspx?link=https://onlinecasino.us.org   http://tellmyhandyman.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://membershipmanagementlinx.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.prvi.hr/modules/mod_jw_srfr/redir.php?url=https://onlinecasino.us.org   http://smallhostel.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://rmschulz.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://diceclearancejobs.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://carolinaconsultationcenter.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
 
 
 
 
 
https://fearl-gaming.com/index.php?topic=509254.new#new
http://vseplotniki.ru/forum/?PAGE_NAME=message&FID=43&TID=733&TITLE_SEO=733-i-think_-that-you-are-not-right.-write-to-me-in-pm_-we-will-discuss.&MID=4735&result=reply#message4735
http://forum.fibroteam.info/showthread.php?p=668187#post668187
https://lindaghill.com/2018/08/15/one-liner-wednesday-snail-mail/
http://sr.leet.cz/viewtopic.php?f=16&t=457