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://www.vpn-zum-ikva-beweisforum.de/memberlist.php?mode=viewprofile&u=306489  https://forum.zenstudios.com/member.php?2527695-Dimmqlkep   http://l2emi.eu/Forum/member.php?action=profile&uid=1295   http://forumbalada.com/member.php?action=profile&uid=22005  http://staysafe-vimmerby.no-ip.org/forum/phpBB3/memberlist.php?mode=viewprofile&u=153469   https://zeropointvoid.com/forum/member.php?action=profile&uid=1448  http://goldwing.ws/member.php/14-WizzaotLax  http://devops-india.com/profile.php?id=117288   https://forum.esforces.com/members/44999-Digdmric 
http://www.fawnisland.com/forum/index.php?action=profile;u=22757  http://www.ifoundyou.org/member/280-wizzayabit   https://forum.awarz.net/member.php?158986-DizzedTusly   http://rift.kyme32.ro/forum/profile.php?id=119859  http://mindmaster.fi/forum/member.php?action=profile&uid=149   https://riiga.net/users/view/dizzuwinore  https://hashtopolis.org/user-111.html  https://mavenhacks.net/member.php?action=profile&uid=58   http://gallery.needforspeed.sk/profile.php?uid=510887 
http://nazaninmousavi.com/index.php?subaction=userinfo&user=WizzdqTok  https://forum.esforces.com/members/44992-Wizzasnric   http://forum.viewbiquity.com/member.php?action=profile&uid=281436   http://fakti.ks.ua/user/Dimmfdbes/  http://dantespit.com/memberlist.php?mode=viewprofile&u=68   https://www.iroids.com/forums/member.php?29405-Dizzhzgloth  http://forum.lexiklopedia.de/member.php?action=profile&uid=166  http://trmconsulting.ca/index.php?option=com_k2&view=itemlist&task=user&id=984784   http://coarte.com.ec/index.php?option=com_k2&view=itemlist&task=user&id=35546 
http://freerollspokerroom.com/member.php?action=profile&uid=15319  http://rahdaran.org/user/WizzzvRence/   https://fxfan.ru/user/Wizzadyneige/   http://www.grandplangaming.com/forum/memberlist.php?mode=viewprofile&u=2888  https://www.affilorama.com/member/wizzvuwef   http://incatalogues.ru/bbs/tools.php?event=profile&pname=DimmaxdernE  http://www.dzzzr.org.ua/forum/member.php?u=142337  https://www.redpah.com/profile/32444/dizzhxdaf   https://www.favor.com.ua/user/WizzasuShouh 
http://mikrotik-tr.com/index.php?action=profile;u=57011  http://www.grandplangaming.com/forum/memberlist.php?mode=viewprofile&u=2996   http://technologydevelopers.co.nf/forum/member.php?80-DigghrWam   http://rkr.in.ua/user/AiidxmSom/  http://pioneeri.ru/forum/memberlist.php?mode=viewprofile&u=84486   http://icedepth.com/forum/profile.php?id=56672  http://7e-it.net/vb/member.php?u=95270  http://www.kran-club.ru/users/Aiidorjam   http://km-schools.edu.sa/vb/member.php?13978-WizzvwFoedo 
http://harwooj.co.uk/Epsommm/member.php?action=profile&uid=4476  http://realtimecheats.com/forum/member.php?action=profile&uid=491   http://www.phoenixtruxx.us/index.php/component/users/?option=com_k2&view=itemlist&task=user&id=862   http://btc-f.org/forum/profile.php?id=104183  http://old.uralgufk.ru/user/Dimmakgendy/   http://pioneeri.ru/forum/memberlist.php?mode=viewprofile&u=84751  http://f1casino79.com/forum/profile.php?id=60416  http://ru.pravoteka24.com/user/DimmdzSmugs/   https://forum.bloodkings.eu/user-614.html 
http://ant0n.net/forums/member.php?action=profile&uid=9  http://konarbabaei.ir/user/WizzarpBoock/   https://vw88yes.com/forum/profile.php?id=215278   https://cinedsofa.li/member.php?action=profile&uid=75  http://forum.outils-conviviaux.fr/member.php?action=profile&uid=50707   http://comic-lair.com/forum/member.php?action=profile&uid=212  http://blackhatcoins.com/member.php/186724-DimmijSaito  https://forum.bulletstormgaming.com/member.php?action=profile&uid=33   http://forum.fibroteam.info/member.php?u=12533 
https://consignmentshoponline.com/author/wizzevsmuch  http://marjorieaperry.com/index.php/component/users/?option=com_k2&view=itemlist&task=user&id=372606   http://sch112.ru/user/Wizzaniglymn/   http://www.speedwayforum.pl/User-digsrdaype  http://chatflh.com/vb/member.php?u=370   http://forum.voetbalzone.nl/member.php?u=16248  http://www.clasifik2gratis.com/author/dizzqustipt/  http://www.tunturisusi.com/aforismitfoorumi/phpBB3/memberlist.php?mode=viewprofile&u=61   http://forum.kolektabot.com/member.php?action=profile&uid=177 
http://angle-des.com/vb/member.php?u=74148  http://knoxxedoutgaming.com.au/forum/member.php?action=profile&uid=425   http://vonwolkenstein.ano-host.co.in/vb4-neu/member.php?78724-Soogezweelm   https://istinastroitelstva.ru/user/WizzyiZoF/  http://www.letechs.com/forum/member.php?action=profile&uid=30690   http://www.askaluminium.com/User-WizzaawBuist  http://hristiqni.site/member.php?6-WizzwdEmumn  http://poster.berdyansk.net/user/WizzacyTauri/   http://forum.englishmountainoutdoors.com/member.php?action=profile&uid=49 
https://forums.laviaccounts.com/member.php?action=profile&uid=950  http://honeykhansar.com/user/DizzaxVaw/   http://xxxvipporno.com/user/Dizzlutrure/   http://wafironline.com/author/wizzafgemomb/  https://www.affilorama.com/member/wizzjgwef   http://mlkj.be/forum/profile.php?id=15136  http://kryllyn.net/forum/profile.php?id=13538  http://www.tunturisusi.com/aforismitfoorumi/phpBB3/memberlist.php?mode=viewprofile&u=53   http://cbsprk.ru/forum/memberlist.php?mode=viewprofile&u=310441 
 
 
 
 
 
http://pd.just-roleplay.com/viewtopic.php?f=4&t=1841
http://htlsr.de/forum/showthread.php?tid=9879
http://hazygame.com/forum/showthread.php/4-testetetetete?p=29#post29
http://missouridom.org/forum/index.php?topic=4691.new#new
http://supportlove.org/forums/topic/local-police-scanner-online/