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://thehealingstudio.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://coronapointe.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://harkinsmoviemonkey.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://greenrobot-apps.net/go/?https://onlinecasino888.us.org  http://www.gunstoreusa.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://petzetakisafrica.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.strahlentherapie-online.com/thread.php?goto=onlinecasino888.us.org  http://www.probiogenic.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://innocentvagina.com/cgi-bin/out.cgi?id=63&l=top&t=100t&u=https://onlinecasino888.us.org 
http://www.hornyteenstube.com/cgi-bin/a2/out.cgi?id=100&l=toplist&u=https://onlinecasino888.us.org  http://tetron.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://stopcyberbullying.org.uk/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.gsfserver.smffy.com/index.php?thememode=full;redirect=https://onlinecasino888.us.org  http://protectthecreation.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://wilderinalaska.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.solutionsrealtynow.com/searchpoint/redir.asp?reg_id=ptypes&sname=/searchpoint/search.asp&lid=1&sponsor=lea&url=https://onlinecasino888.us.org  http://crystaldynamix.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://mashintop.ru/redirect.php?url=https://onlinecasino888.us.org 
http://panerabreadco.us/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.stannswarrington.org.uk/goto-link.php?link_id=11&url=https://onlinecasino888.us.org   http://www.manzano-associates.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://thaidetectives.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://cashsouthern.com/go.php?url=https://onlinecasino888.us.org   http://cindyscleaningtx.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://lunarglobal.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://mediastride.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://7dsearch.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://insider.ru/info.php?a<>=<a href=https://onlinecasino888.us.org panels and battery storage<%  http://mothersexypornvideo.com/cgi-bin/crtr/out.cgi?id=1879&l=topmain&u=https://onlinecasino888.us.org   http://asadjafari.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://cuties4kids.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.surfcity.us/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.herbcn.com/wp-content/themes/begin/inc/go.php?url=https://onlinecasino888.us.org  http://getthefactsokc.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://pascetta.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://rotobrush.biz/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://oysterstreetstudio.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://rushko.ru/out.php?url=https://onlinecasino888.us.org/   http://seniormiles.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://retothek.checkit.ch/Reto/ct.ashx?id=80155b15-4559-4fa8-ac57-839b08fba421&url=https://onlinecasino888.us.org/  http://www.studyireland.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://jowz.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://linkchain.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://mjpetersonmarketing.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   https://www.myregister.gr/el/redirect/?url=https://onlinecasino888.us.org 
http://fins.actwin.com/g.php?t=Algone&u=https://onlinecasino888.us.org  http://www.avls.pt/admin/Portal/LinkClick.aspx?tabid=478&table=Links&field=ItemID&id=991&link=https://onlinecasino888.us.org   http://switchyardproductions.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://youpug.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://roseislandrental.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://farahfamilyonline.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.wichita.edu/thisis/mobilecheck/breakmobile.asp?https://onlinecasino888.us.org  http://www.itftennis.org/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://newmutualistmap.us/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://xocal.com/__media__/js/netsoltrademark.php?d=&http://animation3d.in/pythontraining.net/component/k2/item/4-enim-ac-eleifend-ultricesonlinecasino888.us.org  http://trc.taboolasyndication.com/washpost/log/2/click?pi=http://www.washingtonpost.com/lifestyle/style/art-and-politics-meet-in-collection-of-labor-images/2011/09/07/giqasxgqdk_story.html&sd=v1_a6c0075003c63af6915df98f2bc41e7e_3b2b9b2c-7a4e-498b-9562-cf23fee82879_1315857588_1315857588&ui=3b2b9b2c-7a4e-498b-9562-cf23fee82879&it=video&ii=7094938879832470976&pt=text&li=rbox-t2v&url=https://onlinecasino888.us.org   https://qmobi.agency/blog/go.php?url=https://onlinecasino888.us.org   http://hdporn1.com/cgi-bin/hdp.cgi?c=1&s=70&hdp=1&u=https://onlinecasino888.us.org  https://animations-enfants-hardelot.fr/Statistiques/CountStat.php?url=https://onlinecasino888.us.org   http://granitebayassets.com/New/granitebayassets/vlink.asp?url=https://onlinecasino888.us.org/  http://www.studying.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.kultboy.com/go.php?https://onlinecasino888.us.org   http://e-buildmaterials.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://alticeamerica.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://carerecruituk.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://www.catpoweredtrucks.com/forms/specsheet/emailfriend.aspx?subject=2005%20BLUE%20BIRD%20BB&url=https://onlinecasino888.us.org   https://www.landcruiserclub.net/custom/redir.php?pid=22&url=https://onlinecasino888.us.org  http://www.shakokac.com.hk/__media__/js/trademark.php?d=onlinecasino888.us.org   https://www.shop9val.ru/bitrix/rk.php?id=1&site_id=s1&event1=banner&event2=click&goto=onlinecasino888.us.org  https://centralpanasonic.com.mx/bitrix/rk.php?id=24&event1=banner&event2=click&event3=1+%2F+%5B24%5D+%5B670x120%5D+App+Blu-ray&goto=https://onlinecasino888.us.org  http://www.onlinecity.cn/tools/alexa/Index.asp?url=onlinecasino888.us.org   http://www.nh.lovenet.cn/ienumber.php?action=assign&h=www.nh.lovenet.cn&r=http%3A%2F%2Fwww.nh.lovenet.cn%2Flovenet.php%3FPHPSESSID%3Dv59tt4m2t95pemqhfgpa4823t7%26did%3Dhow_to_receive_more_love_letters&u=https://onlinecasino888.us.org 
http://pimpremium.com/lang.aspx?id_lang=2&url=https://onlinecasino888.us.org  http://www.farleighcandoverandwield.org.uk/goto-link.php?link_id=15&url=https://onlinecasino888.us.org   https://vkne.ru/away.php?to=https://onlinecasino888.us.org   http://www.croatiaguidebook.info/modules/babel/redirect.php?newlang=en_US&newurl=https://onlinecasino888.us.org  http://spinebraces.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   https://www.ptsecurity.com/bitrix/rk.php?id=35&site_id=p9&event1=banner&event2=click&event3=6+%2F+%5B35%5D+%5BSQ_SLIDER%5D+USP_AI_EN_4&goto=onlinecasino888.us.org  https://zhytomyr.bestrest.com.ua/go.php?url=https://onlinecasino888.us.org  http://academysportsandoutdoorsclearance.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://americanhotelhomestore.de/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
http://noodlesexpress.info/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://www.highfrequency.co.nz/ra.asp?url=https://onlinecasino888.us.org   http://hoshodaiko.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://system724.net/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://win.coolclub.it/redirect.asp?id=167&url=https://onlinecasino888.us.org   http://www.portaldajosepaulino.com.br/redirect.asp?cod_cliente=2384&link=https://onlinecasino888.us.org  http://pornshoppingnetwork.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org  http://bkk.biz/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org   http://artslaw.com/__media__/js/netsoltrademark.php?d=onlinecasino888.us.org 
 
 
 
 
 
https://jerry-coleby-williams.net/2018/04/26/market-gardening-vietnam/
http://w.videogame-reviews.net/viewtopic.php?f=10&t=45571708
http://royalmotorscrew.com/viewtopic.php?f=2&t=748
http://www.jjhservers.com/phpbb/viewtopic.php?f=6&t=2316&p=4377#p4377
http://humbit.com/forums/viewtopic.php?f=1&t=789566