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.sgdaohang.com/url.php?url=https://onlinecasino.us.org  http://singularbackup.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://youcantmakethissh-tup.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://motomochila.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://voicevalues.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://pakforum.mobi/go.php?ext_url=https://onlinecasino.us.org  http://www.dokshicy.info/redir.php?https://onlinecasino.us.org  http://pogrzeby-bielsko.firmeo.biz/redir.php?target=onlinecasino.us.org   http://pegasusseyahat.co/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://electronicserviceofprocess.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://56.rjob.ru/bitrix/rk.php?id=1899&site_id=s1&event1=banner&event2=click&event3=3+/+<>899]+<vac>search]+Лавка-Лавка&goto=https://onlinecasino.us.org   https://www.rockstar98.com/out/onlinecasino.us.org   http://bbs.lritat.com/redirect.php?fid=56&tid=11710&goto=onlinecasino.us.org  http://www.greenworker.coop/r?u=https://onlinecasino.us.org   http://bosspedals.com.ua/goto.php?onlinecasino.us.org  http://2913490.r.msn.com/?ld=DvsziGTvAGrbF2rjh0PogFuzVUCUxAJrqy4o5ekqC5ahE3aPd-e_eFsHlEGEzkehvEL1aqdhrE1Jg_dEUHYSM65kFUrrqxDiYEgHzJlpFZwhlHBn7LedanxWeRPGe_B9KVVTAtRj0bIw6hcz3fuQ8uTKiGxEM&u=https://onlinecasino.us.org  http://tomjonesenterprises.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://tonkinsuperstore.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://pnaz.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://bitcoin-faucet.eu/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://jeanbarrylifecoach.maioid.com/cgi-bin/atc/out.cgi?s=60&u=/onlinecasino.us.org   https://www.shifenyangguang.top/odflv/index.php?url=https://onlinecasino.us.org  http://tardukai.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.pbem.fr/feed2js/feed2js.php?src=https://onlinecasino.us.org  http://pcie104.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://www.gccfcats.org/LinkClick.aspx?link=https://onlinecasino.us.org   http://top.062.ru/out.php?www=https://onlinecasino.us.org 
http://38.103.161.168/bbs/redirect.php?fid=231&tid=3101370&goto=onlinecasino.us.org  http://glutenfree-foodies.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://oktaholdings.co/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.lhempire.cn/wp-content/themes/begin/inc/go.php?url=https://onlinecasino.us.org  http://www.ixianzong.com/wp-content/themes/begin/inc/go.php?url=https://onlinecasino.us.org   http://freevirtualrealityporn.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.live-like-a-german.com/redirect.php?redirect=https://onlinecasino.us.org  http://photographyconnects.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.kwaliteitsimpulsgroenalliantie.nl/shortcuts/sendlinkmessage.aspx?url=https://onlinecasino.us.org 
https://login.questrade.com/Signin.aspx?Returnurl=https://onlinecasino.us.org  http://triggerjewels.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.atlantagladiators.com/tracker/index.html?t=sponsor&sponsor_id=48&url=https://onlinecasino.us.org   http://giftplant.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://fiplanpartners.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://o2o.qjwykj.cn/index.php?c=Login&a=index&referer=https://onlinecasino.us.org  http://sorn.eagle-engineering.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://rate-raznoe.dirx.ru/click.php?url=https://onlinecasino.us.org   http://comprarentuciudad.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://realmexfoods.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.algalfuel.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://protective.pcb-manufacturing.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://mbs.mbssecurities.com/Footer/OrderRouting.aspx?goto=https://onlinecasino.us.org  https://www.atterburymuscatatuck.in.ng.mil/LinkClick.aspx?link=https://onlinecasino.us.org   https://www.bertrand-sport-avocat.com/es/droitdusport/index.php?url=https://onlinecasino.us.org  http://51jifenbao.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://trucksellers.com/inventory/specsheet/emailfriend.aspx?subject=2008%20Mitsubishi%20FG&url=https://onlinecasino.us.org   http://u-network.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://robertwelchsociety.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.dialanddudleyfuneralhome.com/cfi/addthis_email.cfm?url=https://onlinecasino.us.org   http://romaguesthouses.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://investc360.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  https://geo.navigator.az/redirect.php?url=https://onlinecasino.us.org   http://anykidcancook.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://choosethespirit.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://dremel-online.ru.xx3.kz/go.php?url=https://onlinecasino.us.org   http://harcourthighschool.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://willowlanecondos.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://vankidok.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://leafguardindianapolis.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://leecanalitoboxing.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://missuniversegermany.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://qualityenglish.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://tomaswhitacre.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://copsforyouth.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://navoigaz.uz/engine/go.php?url=onlinecasino.us.org 
http://www.staging.spaweek.com/redirect.php?spa_id=32132&programid=0&url=https://onlinecasino.us.org  http://hypnosis.tc/__media__/js/trademark.php?d=onlinecasino.us.org   http://dcdcnetwork.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://client.vandaily.com/server/adclick.php?bannerid=210&zoneid=43&source=&dest=https://onlinecasino.us.org  http://centraltransportllc.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://w.ochziffcapital.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://dejdis.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://veteranstradingcompany.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://op.ci/go/?url=https://onlinecasino.us.org 
https://ceriwis.net/forum/out.php?link=https://onlinecasino.us.org  http://teenemployment.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://dishupgrade.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://breezeszanzibar.co.uk/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://justspain.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.stallionsplus.com/external-link.php?link=https://onlinecasino.us.org  http://bhhct.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://localsearch.stargazette.com/sp?cId=0&wId=82505&rNum=1&url=https://onlinecasino.us.org/   http://companionsandhomehelpers.us/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
 
 
 
 
 
http://www.leopoldsdorf-wien.info/phpbb/viewtopic.php?f=2&t=487855
https://traitimbenho.com/threads/2780216-occ-blackboard.html?p=38667630#post38667630
http://mamnontrithuc.edu.vn/forum/showthread.php?43750-add-comment-sale-erectalis&p=191976&posted=1#post191976
http://forum.sonturkrpg.org/showthread.php?tid=88584&pid=141157#pid141157
http://cmamdp.fr/forumM/index.php?topic=11967.new#new