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://hyperionservicesllc.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://twn.northwestmassage.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://m.shopinelpaso.com/redirect.aspx?url=https://onlinecasino.us.org   http://sportybeach.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://qualitytm.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://texascollegiateleague.com/tracker/index.html?t=ad&pool_id=14&ad_id=48&url=https://onlinecasino.us.org  http://caecery.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://pittsburghlaw.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.reality2003.com/akasaka/w3a/redirect.php?redirect=https://onlinecasino.us.org 
http://brandt.nonore.com/go.php?url=https://onlinecasino.us.org  http://www.elempaque.com/b2bportales/rlv.jsp?mail=EMAIL&codigoPortal=EE&numBoletin=2&tipoBoletin=em&url=https://onlinecasino.us.org   http://veloxitas.info/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.aziaten.com/goto.php?https://onlinecasino.us.org  http://www.digojp.com/goto/https://onlinecasino.us.org   http://c2bloc.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://hebosales.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://sibdepo.ru/upload/rk/102/10215005c5ddd14ad871370330430517.swf?flash_link=onlinecasino.us.org   http://sulpizifamily.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://dootychamp.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.nchomeownersalliance.org/r?u=https://onlinecasino.us.org   http://midlandlife.biz/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://vanburencountydem.co/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://cfpsbc.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.kyoto-ogoto.net/s/click.php?tcode=00013928&url=https://onlinecasino.us.org  http://integrityautofinance.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://sunbeachmotel.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.beachesquest.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://jobsinelectronics.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://benschetterer.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://connectmycommunity.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://lainkcasting.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://peteremerson.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://piano-della-localita.ch/url.php?url=https://onlinecasino.us.org  http://poigraem-online.ru/go/url=https://onlinecasino.us.org  http://www.amicadvisor.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://clusters.okidatatonercartridges.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://vivalasvegasweddingreception.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://physics-animations.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://www.dekoracie-vianoce.sk/plugins/guestbook/go.php?url=https://onlinecasino.us.org   http://mcobject.cn/bbs/redirect.php?goto=onlinecasino.us.org  http://www.taankoptometrists.co.uk/index.php?section=1139&tellafriend=go&url=https://onlinecasino.us.org&title=taank   http://virtualairportparking.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.rigidply.ms/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://personaenongrata.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.probiogenic.info/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://www.fishrod.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://gregoryrogerbrown.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://voghouse.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   https://app.primal.com/crm/sales/deals/artificial%20intelligence;information;responses;contacts;author;change;admin;ways?u=https://onlinecasino.us.org  http://www.thefloridadesigngroup.com/seo-scan/onlinecasino.us.org/update   http://mlxinnovia.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://evoceutical.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://facultycentre.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://monument.amathsystems.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://solidaritycenters.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://marketcapcentral.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://nataliawestminster.info/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://atgreenbuild.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://gar86.tmweb.ru/bitrix/rk.php?goto=https://onlinecasino.us.org   http://wap95.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://av.myauto.by/away.php?url=https://onlinecasino.us.org  http://www.boardchannels.com.cn/redirect.php?goto=onlinecasino.us.org   http://www.1-888-shelter.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://vegeliciouscafe.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://г‚¦г‚Јгѓігѓ–гѓ«гѓ‰гѓі.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://fwmedtech.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://vossconsult.co.nz/ra.asp?url=https://onlinecasino.us.org  http://governanceoncloud.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://resellerservices.co.uk/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.noconventions.mobi/x!/go.php?https://onlinecasino.us.org  http://oneautodr.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://rahn-ag.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
http://analbac.fr/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://itv-goggles.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.selektimmo.com/banniere/adclick.php?bannerid=224&zoneid=0&source=&dest=https://onlinecasino.us.org   http://yjams.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.gourmetstyle.ru/bitrix/rk.php?goto=https://onlinecasino.us.org/   http://sportingeyes.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://paymentgram.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://www.072550.cn/goto.php?url=onlinecasino.us.org   http://iqorams.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org 
https://fumcseguin.com/System/UserProfile.asp?id=60278&func=signup&referer=https://onlinecasino.us.org  http://gotjax.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.no1dianji.com/index.php?m=poster&c=index&a=poster_click&sitespaceid=1&id=129&url=https://onlinecasino.us.org   http://dutplanet.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://atouchofelegancelimo.net/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://icwgrp.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://clusterz.com/__media__/js/netsoltrademark.php?d=onlinecasino.us.org  http://w.ptsf.org/__media__/js/netsoltrademark.php?d=onlinecasino.us.org   http://www.cslregister.com/forum/showthread.php?goto=onlinecasino.us.org 
 
 
 
 
 
https://sample.baapstore.com/forum1/index.php/topic,5475.new.html#new
http://asomatous.net/forum/index.php?topic=12003.new#new
http://graphfolio.com/forum/index.php?topic=52192.new#new
http://taiwan.yam.org.tw/womenweb/bookclub/forum1/edit-response.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/womenweb/bookclub/forum1/get.cgi/newreply.php
https://lindaghill.com/2018/08/15/one-liner-wednesday-snail-mail/