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.aimbp.com/__media__/js/netsoltrademark.php?d=&ampcbdoilmarketusa.com  http://alinalyons.com/redir.php?url=https://cbdoilmarketusa.com   http://fc.standardandpoors.com/sites/client/tdanl/TDANewsletter/link_track.vm?topicId=sep10&force=true&source=pdf&redir=https://cbdoilmarketusa.com   http://tcz.wordcards.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://old.sksu.su/go/url=https://cbdoilmarketusa.com   http://www.momsnboys.com/cgi-bin/at3/out.cgi?id=242&tag=toplist&trade=https://cbdoilmarketusa.com  http://americanlottopool.org/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://www.retrotubeclips.com/acj/out.php?id=146&tag=toplist&trade=https://cbdoilmarketusa.com   http://gemclassics.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com 
http://littlesmartypants.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://edonbas.ru/go/url=cbdoilmarketusa.com   http://guysgear.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://unfinishedbusinesswithgod.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  https://141129485.r.bat.bing.com/?ld=d3B0rH7gwinBV3WWibQcVdtDVUCUxM4Gs9o_t2JAU__E79vHfQowlUkR47aF677Qes_b6A3x89dY5QJHiCrPkGmgeXIRUoAqRIQrEvpeeXHf_a14XZc192W3UlH7QukruWqFhdePJ-Fn-r9g7lnhpAwnQIODQvuQP2T8NhY6wSC4FSBpp7&u=https://cbdoilmarketusa.com   http://chefbaja.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://usaroof.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://lexingtoncreditspecialists.org/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://parallelseo.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com 
http://capecoral.eztouse.com/redirect.asp?weburl=cbdoilmarketusa.com  http://duniadropship.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://thehotwaterstore.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://fordebtreliefnow.org/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://www.prontolanka.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://facemeyerlaw.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://fhq.czechair.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://vacubagit.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://nepredators.com/Goto.asp?url=https://cbdoilmarketusa.com 
http://ifrecrealestateschools.info/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://cndongkai.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://xrosspoint.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   https://www.theresamah.com/r?u=https://cbdoilmarketusa.com  http://facialrape.com/cgi-bin/out.cgi?id=735&l=top_top&u=https://cbdoilmarketusa.com   http://www.lesbian-girlfriends.com/cgi-bin/out.cgi?ses=4G2e6Zmrd2&id=128&url=https://cbdoilmarketusa.com  https://ad-log.net/link.php?i=561e2df9139b1&m=570f5d053502c&guid=ON&url=https://cbdoilmarketusa.com  http://sanfranciscowellnesscoaching.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://www.allynews.com.ar/tl.php?p=125/119/rs/52f/tj/rs//https://cbdoilmarketusa.com/ 
http://www.tlilian.com/url.php?url=https://cbdoilmarketusa.com  http://pdsmeters.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   https://173097178.r.bat.bing.com/?ld=d3lp0vksE6FavSD6zTyWvMHDVUCUxR1vO34n4b8sLkt7wg-pncsavNc8F7wHubfa8EhSJsi07Xi-NQP-ybaHfY8M06OdtGmN-Wtr3BocTx509Ie1-YW3kucJVp11a03YyZnfvGIFenEAqjPCp4gRY9LbLsAoU&u=https://cbdoilmarketusa.com   http://xy720a.boiea.cn/dq/go.php?url=https://cbdoilmarketusa.com  http://www.peigeo.re/accueil/envoyer_a_un_ami?url=https://cbdoilmarketusa.com   http://cantech.us/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://packageyourdevice.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://mypocketelections.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   https://zhilan.net/url.php?url=https://cbdoilmarketusa.com 
http://streetpilot.info/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://sanfranciscoarena.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://www.geotrans.md/banner_sys/redirect.asp?id=364&url=https://cbdoilmarketusa.com   http://jctxwx.info/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://bkbtf.org/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://www.doctor-navi.com/cgi/hospital-search-engine2/rank.cgi?mode=link&id=14979&url=https://cbdoilmarketusa.com  http://proactivesurveysolutions.mobi/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://www.tendencias-moda.com/feedcanalmail.php?location=https://cbdoilmarketusa.com/   http://digitalindustries.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com 
http://kidskool.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://mytechprofessor.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://indyracing.org/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://yct.rfmonitor.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://undercovergirlenterprises.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://terni.azinet.it/aziende/clienti-sicuri-aziende-privati-pregiudizievoli-protesti/49218/link.php?id=49218&link=https://cbdoilmarketusa.com  http://www.julieharrisphotography.com/blog/go.php?https://cbdoilmarketusa.com  http://www.esp-cloud.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://airfred.org/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com 
http://yourchevydealeronnorthlake.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://ushealthcarejobs.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://lyj.7worldtradecenter.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://alimama-group.info/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://gesuender-abnehmen.com/index.php?t=rview&th=5719&goto=cbdoilmarketusa.com   http://captainjoe.biz/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://kumakun.com/cgi-bin/kboard.cgi?mode=res_html&owner=proscar&url=cbdoilmarketusa.com  http://dom-press.com/go.php?url=https://cbdoilmarketusa.com   https://cucinadisartini.com/_m/index.php?a=free_page/goto_mobile&referer=https://cbdoilmarketusa.com 
http://logi141.xiti.com/go.click?xts=322451&s2=1&p=doc&clic=S&type=click&url=https://cbdoilmarketusa.com  http://specialtyminerals.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://paineweber.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://www.hospitalmetropolitano.com.br/includes/go.php?url=cbdoilmarketusa.com  http://axevideos.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://rob.teamcaller.net/login.do?go=https://cbdoilmarketusa.com  http://www.talgals.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://aicglobal.us/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://www.ims-padds.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com 
http://www.hotuna.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://missuniverseturksandcaicos.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://www.cardinalseanblog.org/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://ppgadvisors.co.uk/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://ownclassified.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com   http://targetinsurance.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://handurance.net/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com  http://www.webmovye.gq/out.php?l=https://cbdoilmarketusa.com   http://thepowerofseniors.com/__media__/js/netsoltrademark.php?d=cbdoilmarketusa.com 
 
 
 
 
 
http://taiyi.one/viewtopic.php?pid=274781#p274781
http://niklever.com/spritoon/forum/viewtopic.php?f=5&t=109811
https://www.wasdspace.ro/forum/showthread.php?tid=18655
http://forum.gta-v-trucking.com/index.php?topic=3.new#new
http://www.nongyai101.org/webboard/index.php?topic=2277.new#new