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://successfully.efolks.am/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://dreamercenter.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://knowmylanpd.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://gooddivorcelawyers.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  https://olshy-tech.pl/modules/mod_jw_srfr/redir.php?url=https://onlinecasino2018.us.org   http://fanpier.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://accesssanmiguel.com/go.php?item=1132&target=https://onlinecasino2018.us.org  http://logc279.xiti.com/go.url?xts=525132&xtor=AD-1-<DE>-<>-<logo>&url=https://onlinecasino2018.us.org   http://www.mayi580.com/ids.mayi580.com/amserver/UI/Login?goto=https://onlinecasino2018.us.org 
http://theofficialsites.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://tokyo-f.net/s/click.php?tcode=00010657&url=https://onlinecasino2018.us.org   http://www.netzpro.in/retailless/discounts/onlinecasino2018.us.org   http://vintagenewsmagazine.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  https://www.ugnsecurity.com/link.php?system=redirect&url=https://onlinecasino2018.us.org   http://thebilliondollarsmile.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.legapro.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.reefs.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://panes.securehostingcenter.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
http://bhhyostandlittle.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.bartonandloguidice.com/LinkClick.aspx?link=https://onlinecasino2018.us.org   http://www.genevaaz.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://progrentis.com.br/inicio/tabid/109/ctl/sendpassword/default.aspx?returnurl=https://onlinecasino2018.us.org  http://maximummaximiles.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   https://secure.winspark.com/index.html?has_js=1&brandId=3&loa=load&referer=https://onlinecasino2018.us.org  http://g4a.wire-vac.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://easy-fitgreasetraps.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://m.shopinminneapolis.com/redirect.aspx?url=onlinecasino2018.us.org 
https://www.medier24.no/auth/?module=TekAuth&service=logout&referer=https://onlinecasino2018.us.org  http://www.mac4us.nl/redirect.php?action=url&goto=onlinecasino2018.us.org   http://www.facialacne.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://tousatsudou.select-chann.com/link.php?url=https://onlinecasino2018.us.org  http://joshdev.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://ucd.uz/upload/rk/27e/ICTExpoj200x300_RUx.swf?flash_link=onlinecasino2018.us.org  http://mavenrazor.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://westcoastmusicians.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://searchinthecity.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
http://www.americolife.tc/__media__/js/trademark.php?d=onlinecasino2018.us.org  http://oxy-health.info/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://eliopoulos.us/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://www.website-guide.com/redirect.php?url=https://onlinecasino2018.us.org  https://ctweb.ru/redirect.php?goto=https://onlinecasino2018.us.org   http://yl.gmlxin.com/scenic.php?g=Scenic&c=Recognition&a=see_login_qrcode&referer=https://onlinecasino2018.us.org  http://fuckuandurfeelings.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://canineaction.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://www.aura.kz/bitrix/rk.php?id=27&goto=https://onlinecasino2018.us.org 
http://donayresphotography.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://elderly-carejobs.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://mybesttop10.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://haanshop.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://images.google.co.uz/url?q=https://onlinecasino2018.us.org   http://infostart.by/redirect.php?url=onlinecasino2018.us.org  http://www.riuqp.com/bbs_0731go_cn/connect.php?mod=login&op=init&referer=https://onlinecasino2018.us.org  http://doneck-news.com/engine/go.php?url=onlinecasino2018.us.org   http://ecorporaterealestate.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
http://platina4.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://bootikeman.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://www.stevensoncancercenter.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://imgmedia.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.hussonu.com/redirect?SessionGuid=55b0635b-a77d-495a-a915-4dde13f05dbe&url=https://onlinecasino2018.us.org   http://www.reg3.ru/url.php??s=https://onlinecasino2018.us.org  http://winemakingfun.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://www.oliveoilshop.it/redirect?action=url&goto=https://onlinecasino2018.us.org   http://bauer.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
http://club.mansinton.com/redirect.php?tid=91&goto=onlinecasino2018.us.org  http://dadsflix.com/cgi-bin/a2/out.cgi?id=815&u=https://onlinecasino2018.us.org   http://nprsongoftheday.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://search.clarionledger.com/sp?cId=2947408&wId=121610&rNum=24&url=https://onlinecasino2018.us.org  http://www.osservamedia.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   https://storage.athlinks.com/logout.aspx?returnurl=https://onlinecasino2018.us.org/  http://kenwood-telecom.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://sns.1dugo.com/link.php?url=https://onlinecasino2018.us.org   https://www.firstchoicecu.org/redirect.php?link=onlinecasino2018.us.org 
http://teenporn-pictures.com/crtr/cgi/out.cgi?id=55&l=top_top&u=https://onlinecasino2018.us.org  http://hotelsmyconos.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://limefreshfranchise.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://www.chanon.ro/l.php?r=https://onlinecasino2018.us.org  http://glgoo.jsylhs.com/url?q=https://onlinecasino2018.us.org   http://buynuheat.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://aiworkbench.org/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://izimap.ru/?company=3687onlinecasino2018.us.org   http://da-mao-miami.xoxohth.com/go.php?url=https://onlinecasino2018.us.org 
http://besso.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://uapress.info/uk/go?url=https://onlinecasino2018.us.org   http://bbs.pingtan.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://vgx.cadquality.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://spp.wmpolicyissued.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://africanative.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org  http://activeebonygirls.com/?go=click&c=10&n=31&e=0&g=2&r=11875&u=https://onlinecasino2018.us.org  http://kamarelldesign.com/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org   http://oclibraries.net/__media__/js/netsoltrademark.php?d=onlinecasino2018.us.org 
 
 
 
 
 
http://zohra.ch/forum/viewtopic.php?p=118474#118474
http://www.theshiftseries.com/vandnika-sookrit-of-the-leaders-forum/#comment-374
http://worldcup062018.com/worldcup/viewtopic.php?pid=36172#p36172
http://ticket.edu.vn/showthread.php?67698-the-trip-stephen-hawking&p=75651#post75651
https://celsohl.com/?contact-form-id=50235&contact-form-sent=96052&contact-form-hash=61b7306e80c71013c4456a98c4fbb64f390ec1fb&_wpnonce=2923da2649