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://uvirinc.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  https://ie.fukushima-sumai.com/jump.php?curl=https://onlinecasinodd.com   http://selectafax.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://bielkovinovadieta.nuevobancosantafe.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://tignon.biz/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://shoa-nurse.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://wisconsinnativeplants.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://biyougeka.esthetic-esthe.com/rank.cgi?mode=link&id=848&url=https://onlinecasinodd.com   http://miners-pool.comedyclips.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://www.adintrend.com/banner_stat/click.php?banner_name=TopBHL_281113_SS_BTS&url=https://onlinecasinodd.com  http://getedfunding.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.91game.com/go.php?url=https://onlinecasinodd.com   http://oxygenpillow.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://dncdirector.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://modabee.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://bk43.ru/links.php?go=https://onlinecasinodd.com  http://www.grassrootseast.org/r?u=https://onlinecasinodd.com   http://ricknelsonthemovie.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://c0.hitta.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://centralpark.ops.org/LinkClick.aspx?link=https://onlinecasinodd.com   http://finalfloor.info/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://waterbysubscription.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://drrichardhall.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://onenesscanada.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://blp.2seeclear.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://bootikeman.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.islandlaserskincare.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://iorigami.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://elmariachitacoshop.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://tucans.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.qqtxt.com/index.php?m=comment&c=index&a=init&commentid=content_16-2963-1&title=%E5%BE%AE%E4%BF%A1%E7%BB%91QQ%E9%A2%86%E9%9A%8F%E6%9C%BA%E5%A4%A9%E6%95%B0%E8%B6%85%E7%BA%A7%E4%BC%9A%E5%91%98&url=https://onlinecasinodd.com  http://tradeext.us/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.paginainizio.com/html_iframe/news/rss.php?rssurl=https://onlinecasinodd.com  http://alimama-corp.mobi/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://tidallive.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.oneclickonly.de/redirect.php?url=https://onlinecasinodd.com 
http://u-pick.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://infodna.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://zzjwfh.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://hobogame.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://selfx.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.ich-habe-ein-pferd.com/Inserate/UserEntries.aspx?returnurl=https://onlinecasinodd.com  http://transformationnationonline.biz/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://jctcuonline.org/redirect.php?link=https://onlinecasinodd.com   http://mitral221.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://www.superoffers.com/click/?merchant=TSC+Toys&partner=17&r=https://onlinecasinodd.com  http://inspireos.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://greatwhitemarineservices.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.elcarteltv.biz/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://pershingllc.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://bluefinrestaurant.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://buffalotours.be/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.otto-office.com/de/redirect.obtshop?wkid=BOTGODEYvqhACZnG9McKQfE&url=https://onlinecasinodd.com   http://cobbmountain.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://weiserlock.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://tkyf28.com/__media__/js/trademark.php?d=onlinecasinodd.com   http://search.geass.info/rank.cgi?mode=link&id=3154&url=https://onlinecasinodd.com/   http://centraleuropeexperts.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://concept-1.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://ideasbc.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://altapacificbank.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://sexydulhan.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://golfsonar.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://ihead.ru.xx3.kz/go.php?url=https://onlinecasinodd.com  http://miniexercise.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://nymphomation.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://commercialbeveragerefrigerators.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://kaeserblogs.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://piratalondon.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://ip-66-51-127-25.tera-byte.com/rotor/link.php?gr=1&id=bb7de8&url=onlinecasinodd.com  http://www.wakqw.com/wp-content/themes/begin5.2/inc/go.php?url=https://onlinecasinodd.com   http://worldoffreesynth.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://f1rstclaim.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://miraimedia.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.perfomania.com/default.aspx?control=artist&name=julie_kagan&url=onlinecasinodd.com   http://nucorjfemexico.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://nwcenterfoundation.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://alpedhuezbike.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://hirgyujto.hu/tart/redirect/2858195?url=https://onlinecasinodd.com  http://www.callcenterclassifieds.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.tiresgames.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://brandenthusiasm.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://jiancaopingjiedao.tieba01.com/go.php?https://onlinecasinodd.com   http://plaindealer-sun.com/Redirect.asp?UID=38375680&SubSectionID=85&AdArrayID=86&AdPosition=-1&LinkURL=https://onlinecasinodd.com   http://plugin.mediaget.com/promo/?url=https://onlinecasinodd.com/  http://www.bitsofbuzz.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   https://disort.ru/bitrix/rk.php?id=14&site_id=s2&event1=banner&event2=click&event3=2+%2F+%5B14%5D+%5BMAIN%5D+%CB%E0%EC%E8%ED%E0%F2+%E0%EA%F6%E8%E8+%E8+%F1%EA%E8%E4%EA%E8&goto=onlinecasinodd.com  http://innovativeoffers.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.mmobid.com/forum/showthread.php?2018-Compra-Galotam-En-Linea&goto=onlinecasinodd.com   http://www.sportsarmory.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
 
 
 
 
 
http://taiwan.yam.org.tw/womenweb/bookclub/forum1/edit-response.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/newreply.php
https://www.michiganalliancegroup.com/forum/showthread.php?tid=650837
http://www.theparrotcentre.com/forum/index.php?topic=38916.new#new
https://forexedu.ro/forums/topic/commander-aciclovir-en-ligne-belgique-generique-prix/page/13/#post-90275
https://yancearizona.net/2010/04/17/pendaftaran-wilayah-adat-belum-jelas/