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)

https://forums.codewalkers.com/php-coding-7/phpmailer-error-could-not-instantiate-mail-function-720775.html?goto=onlinecasino.us.org  http://anmolgold.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.expertplus.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://online.kaplanit.com/redir.asp?link=https://onlinecasino.us.org  https://www.thedreamcorps.org/r?u=https://onlinecasino.us.org   http://barbarastreisand.biz/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://ns2.vcvps1867.vcdns.de/test.php?a<>=<a+href=https://onlinecasino.us.org  http://www.ua.all.biz/goto.php?url=https://onlinecasino.us.org   http://teknogunluk.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://cntumor.cn/bbs/redirect.php?tid=128099&goto=onlinecasino.us.org  http://newyorkbreadexpress.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://catanzaro.azinet.it/aziende/servizi-taxi-noleggio-aeroporto-lamezia-terme/47256/link.php?id=47256&link=https://onlinecasino.us.org   http://alive-ua.com/engine/go.php?url=onlinecasino.us.org  http://etw.givemebooks.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.hotelprincipepesaro.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://xej.800steamer.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://images.google.co.ls/url?q=https://onlinecasino.us.org   http://www.timforconnecticut.com/r?u=https://onlinecasino.us.org 
http://rcsand.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://928583.r.bat.bing.com/?ld=d3Cregh0GJ5wxl82OkCqrSNjVUCUw98wh8og7eJnsqNfv48mKppNu4QIe1aqRMc6UgkHSOa-UvGimLWxvXwELRrGgxprfyLTBvD7s-OrHCk_C1OiFHDjiELk7bUjVX14OgHRVIpDGVGhjPge6KYHpu6cXjBYQ&u=https://onlinecasino.us.org   http://www.33kvm.ru/go.php?url=https://onlinecasino.us.org   http://ecaatest.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://nedrafoster.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://namba.kg.xx3.kz/go.php?url=https://onlinecasino.us.org  http://texasgirls.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://www.netwalkerstore.com/Redirect.asp?AccID=-18244&AdCampaignID=2991&AdCampaignType=2&AffDuration=30&url=https://onlinecasino.us.org   http://alouette-elegante.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://www.nkez.net/Link/index.asp?action=go&fl_id=12&url=https://onlinecasino.us.org  http://hookedontheoutdoors.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.illinoisworknet.com/vos_portal/residents/en/admin/landing.htm?pred=https://onlinecasino.us.org   http://aytenozer.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://befanarecords.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://cowvip.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://nmyardcare.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://jackbinion.biz/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.assocamerestero.net/Dati/visitexe.asp?IDDato=92129&IDUS=undefined&goto=https://onlinecasino.us.org 
https://2714498.r.bat.bing.com/?ld=d3GQZeoM8LZU9uQvTDzrYrgjVUCUxFhVgK5RKD70CI3iIbeQ_Fe4OHmXr2sUcAf7OOWnnz8jCyFqipZS0Hd97siUur31zc6awAy0tp50UukjKPKbxjlhJ65cHdepvp8j8k40ojnGBlupJ9m3O2BNN7pjoNdyj-8uC5JZBas3LKLBo2P5_h&u=https://onlinecasino.us.org  http://yesformaps3.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://make-games.ru/go?https://onlinecasino.us.org   http://www.thekarupspc.com/crtr/cgi/out.cgi?id=72&tag=top_footer&trade=https://onlinecasino.us.org  http://danielsonharrigan.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://allformetv.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://crimea.kp.ru/go/https://onlinecasino.us.org  http://thechickenbaby.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.greatdirectories.org/redir.php?url=https://onlinecasino.us.org 
http://www.tastytom.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://winterolympia.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://rokkan.co.uk/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.sctc.net/dnn/LinkClick.aspx?link=https://onlinecasino.us.org  http://stixsoftware.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://apb5.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://hashari.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://globalhealtheduonline.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://piumftp.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://loadedsugar.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.rusenergy.com/bitrix/rk.php?goto=https://onlinecasino.us.org   http://www.iraqag.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.newwealth.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://brokerstest.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://invitro.kz/bitrix/rk.php?id=338&event1=banner&event2=click&event3=1+%2F+%5B338%5D+%5BFOR_CLIENTS%5D+%CF%F0%EE%E2%E5%F0%FC+%97+%ED%E5+%EF%EE%F0%E0+%EB%E8+%E2+%EE%F2%EF%F3%F1%EA%2C+%CA%E0%E7%E0%F5%F1%F2%E0%ED&goto=onlinecasino.us.org  http://betafinance.ru/redirect.php?url=https://onlinecasino.us.org  http://pim.mattpaulson.com/subscribepopup.aspx?RegistrationCode=NorthForkVue Popup&domain=onlinecasino.us.org   http://www.macdigger.ru/goto/https://onlinecasino.us.org 
https://www.l0n.net/redirect.php?link=https://onlinecasino.us.org  http://halfcenter.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://tenstarretreats.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://xn--c1aedhi4ahjj.xn--p1ai/bitrix/rk.php?id=8484&goto=onlinecasino.us.org  http://www.malaskyproperties.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://tangohot.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://waralbum.ru/goto/https://onlinecasino.us.org  http://beerparty.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.catalystnyc.com/__media__/js/trademark.php?d=onlinecasino.us.org 
http://beyaz.az/go.php?url=https://onlinecasino.us.org  http://www.aktualnizpravy.info/go.php?u=https://onlinecasino.us.org   http://www.merle-norman-dayspa.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://heartland-housecalls.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://aposter.biz/redirect.php?url=https://onlinecasino.us.org   http://iceappliance.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://wholestock.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://osmcheatmobile.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://individualware.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://sportsworkers.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://mountprospect.org/redirect.aspx?url=https://onlinecasino.us.org/   http://www.akwaibomnewsonline.com/news/index.php?url=https://onlinecasino.us.org   http://versteckehn.tk/link?r=2057349&t=11&s=70&l=thumbs&u=https://onlinecasino.us.org  http://gobild.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://okla-adventist.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://pursuitofwine.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://renegadeninja.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://vandervlugt.info/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
 
 
 
 
 
https://treasuredcollections.ca/ppug/viewtopic.php?f=3&t=9495
https://smallhousebliss.com/2013/01/16/vacation-cottage-in-denmark-by-moen-huset/
http://www.rmlalliance.com/forum/index.php?topic=4039.new#new
http://www.conspiracawh.com/viewtopic.php?f=9&t=28
http://lycee-marxdormoy-creteil.fr/forum/viewtopic.php?f=2&t=6703