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.tropicalcocktails.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://ei-santГ©.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.amestana.lt/url.php?url=https://onlinecasinodd.com   http://fran-chocolate.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://weatherisfun.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://disabilities-law.us/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.easttexansforliberty.com/r?u=https://onlinecasinodd.com  https://soaptv.me/engine/go.php?url=onlinecasinodd.com   http://www.laureloak.com/default.aspx?p=TrackHyperlink&url=https://onlinecasinodd.com 
http://asspros.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://tuttoeros.e-z-go.biz/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://x1x.click-chann.com/link.php?url=https://onlinecasinodd.com   http://yc.xinshizaixian.com/index.php?g=Appoint&c=Changecity&a=go&city=xsbn&referer=https://onlinecasinodd.com  http://miopportunity.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.robbinsresearchinternational.biz/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://maps.google.dz/url?q=https://onlinecasinodd.com  http://slightly-bigger.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://fromthelens.ca/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
https://141007426.r.bat.bing.com/?ld=d3R1Cxl5UR-Fhgq_yDSg5pITVUCUxLXUhnd5fEnTZjjstJR6NtVdcA9Fqn89yfATlXjdajVJkjYQrQcCcZadYd-TpU9EIGhvlfh4hvbn8shtFvajtoQY8SF1UWV_9xwicg4zQUYA_e7sCsIGXBxYRsBd4B5dEmRO3C3Wn4t-NYO6f42DbS&u=https://onlinecasinodd.com  http://reeboksportsclub.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://asgrandnord.e-monsite.com/liens/do/redirect/?url=https://onlinecasinodd.com   http://clara-indiana.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://pottypress.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.heartsrizing.com/cgi/mt/mt4i.cgi?id=7&mode=redirect&no=90&ref_eid=1203&url=https://onlinecasinodd.com  http://ashburndogpark.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://cletheatre.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://tfccsr.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://rometers.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  https://frackfreeplanet.com/c?u=https://onlinecasinodd.com   http://tessenderlotrading.cn/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://courtneyblackman.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.urban-offroad.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://m.greenchoicecarpet.com/?url=https://onlinecasinodd.com  http://totaralms.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://rasot.gov.ua/bitrix/rk.php?id=21&event1=banner&event2=click&event3=1+%2F+%5B21%5D+%5BPARTNERS_B%5D+&goto=https://onlinecasinodd.com   http://www.michaelpalmerbooks.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://lsinternal.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://dnepropetrovsk.restoranchik.com.ua/bitrix/redirect.php?event1=rest_out&event2=Array&event3=%CF%E0%ED%EE%F0%E0%EC%E0&goto=https://onlinecasinodd.com   https://www.ruimtevlaanderen.be/LinkClick.aspx?link=https://onlinecasinodd.com   http://mouseembryonicstemcellmedia.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.novalogic.com/remote.asp?NLink=https://onlinecasinodd.com   http://boycott-this.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.azcourts.gov/LinkClick.aspx?link=https://onlinecasinodd.com  http://thegreatestrivalryinsports.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   https://sterlitamak.ru/bitrix/redirect.php?event1=bnr_vitrina&event2=67084&event3=&goto=https://onlinecasinodd.com 
http://www.northlandsensoryinsights.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://flexpowerone.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://rcuniversity.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.on-siteresponse.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://yellowpagespro.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.backbytes.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.hampelmail.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://magazinereps.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.friends-project.de/wbblite/thread.php?goto=onlinecasinodd.com 
http://www.leisureolympics.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://summitco.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://rivervalleyopenhouses.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://likeaprice.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://track.efiliacao.com.br/servlet/effi.redir?id_compteur=11710603&url=https://onlinecasinodd.com/   http://97a.hungerbanquet.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://charmedivino.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://vinthropology.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.conanexiles.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://www.ksmsw.com/index.php?c=reg&&referer=https://onlinecasinodd.com  http://architecturalwooddoors.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://aarpinsurence.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   https://forexdengi.com/threads/43096-galleass-capital?goto=onlinecasinodd.com  http://technoengineer.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://members.mijente.net/r?u=https://onlinecasinodd.com  http://zenithsystems.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://lexingtonelife.mobi/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://cignatureleasing.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
http://bestitched.info/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://ehobs.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://cognitonyc.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://r.app.xiaomi.com/login?referer=https://onlinecasinodd.com  http://www.caldwelldifilippos.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://143guys.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://2p2.undclubs.org/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://perceptiveinvesting.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.boxeri.ackovosrot.cz/redirect.php?url=https://onlinecasinodd.com 
http://www.adeptera.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://vision-star.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://www.swimbaitnation.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://oprahcruisecenter.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://www.msighe.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://clearwaterchildrens.com/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://ganleyapp.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com  http://chemistryonline.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com   http://vlaanderen.net/__media__/js/netsoltrademark.php?d=onlinecasinodd.com 
 
 
 
 
 
https://farreroboom.ml/viewtopic.php?pid=5684#p5684
https://coinping.co/btc/viewtopic.php?pid=178193#p178193
http://slpsfd.org/forum/index.php?topic=27646.new#new
http://community.alahlyegypt.com/forum/main-forum/139579-graduation-yearbook-sayings
http://certare.pl/forum/viewtopic.php?f=36&t=243118&p=423091#p423091