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://beamentor.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://www.xwave.cn/go.asp?url=https://cannacbdoilrx.com   http://fordgtcentral.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://deerfacts.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://ibanpayment.us/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://moeobrazovanie.ru/redirect.php?to=https://cannacbdoilrx.com  http://at.catalog.hartmann.info/hartmann-shop/login/login.html;jsessionid=TbWsdU2iXNQ7BmYdn7jOL+6b?referer=https://cannacbdoilrx.com  http://bdj.donateyourcartoday.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://cobbdailydeals.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://nuimagesurgery.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://go.cartoon-dl.ir/transfer/index.php?url=https://cannacbdoilrx.com   https://swaeq.com/vb/showthread.php?goto=cannacbdoilrx.com   http://reservationsgreatwolflodge.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://www.voterchoicema.org/r?u=https://cannacbdoilrx.com   http://hemispheresolutionsltd.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://qgg.badforgood.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  https://www.stop-book.com/bitrix/redirect.php?event1=news_out&event2=http%3A%2F%2Fwww.compromat.ru%2Fpage_36751.htm&event3=+%D7%E8%ED%EE%E2%ED%E8%EA%E8+%E8+%E8%F5+%F1%E5%EC%FC%E8.+&goto=https://cannacbdoilrx.com   http://tzblm.com/engine/go.php?url=cannacbdoilrx.com 
http://m.kotlas-info.ru/vustug-info/away.php?to=cannacbdoilrx.com  http://spectrumhealingartscenter.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://3gain.net/link.php?id=N0000182&adwares=A0000001&url=https://cannacbdoilrx.com   http://www.uesoundadvice.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://bcbusinessbrokers.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://theausinstitute.nationbuilder.com/r?u=https://cannacbdoilrx.com  http://bookmarketingideas.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://flatpack.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://axa-advisors.biz/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://a1chimneyservice.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://www.jalight.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://www.tjyoga.com/bbs/redirect.php?fid=74&tid=34&goto=cannacbdoilrx.com   http://wuv.makehimknown.org/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://belletrist.org/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://marshfieldclinic.co/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://drumparties.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://michaelkinney.ca/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://thrillworkz.org/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://pfchangscareers.us/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://mollybelle.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://comicchats.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://www.starlightdancesport.com/dnn/LinkClick.aspx?link=https://cannacbdoilrx.com  http://mm2lm9262.youpost.info/redirect.php?tid=3897&goto=cannacbdoilrx.com   http://p.somode.net/AP5Min.aspx?AID=20693&Auth=DDD31BD6A08A61B9E9583C5B9736446C28443B941E952073DDC88CE1A74F6C17&referer=https://cannacbdoilrx.com  http://jewelryboxshop.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://xrq.auto-point.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://filtre.nonore.com/go.php?url=https://cannacbdoilrx.com 
http://wayofhelp.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://202.115.72.51/reserve_sp.jsp?goto=https://cannacbdoilrx.com   http://charterlaunch.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://mainstreetawards.org/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://lakitawright.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://directvahomeloan.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://katahdin.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://maternite.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://www.mata-l-23.com/jump.php?url=https://cannacbdoilrx.com 
http://acinre.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://waste-technology.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://usmuscle.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://www.lautanindonesia.com/forum/go.php?url=https://cannacbdoilrx.com  https://www.pricefetcher.com/redirect?web?s=https://cannacbdoilrx.com   http://joshuawoodruff.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://eprog.net/clicks_inc.php?id=25052&click=2&url=https://cannacbdoilrx.com  http://www.notredamesports.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://theglobalcitizen.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://mothergoose.webfactional.com/r?u=https://cannacbdoilrx.com  http://theriverflows.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://www.toronto350.org/r?u=https://cannacbdoilrx.com   http://friantwaterline.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://www.ev.rsnow.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://bleadon.2day.ws/bleadon/search/?url=https://cannacbdoilrx.com  http://baldwinfacts.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://hearing-aids-university-heights-oh.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://www.unitedship.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://g2labs.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://bluehousecapital.pl/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://ohtopdocs.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://rarebookappraiser.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://wulffies.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://healthclick.tc/__media__/js/trademark.php?d=cannacbdoilrx.com  http://networkofone.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://utnecast.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://www.plasticcactus.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
http://tciresortrentals.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://www.minbanc.org/__media__/js/trademark.php?d=cannacbdoilrx.com   http://pikecorporation.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://theawkwardstage.net/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://www.almazon.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://antinauseawristbands.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://costcocaskets.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com  http://jouett.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com   http://proofer1.com/__media__/js/netsoltrademark.php?d=cannacbdoilrx.com 
 
 
 
 
 
http://computernetworktutor.com/fluxbb/viewtopic.php?pid=618556#p618556
http://300house.com/forum/viewtopic.php?f=14&t=3897
https://www.the-ninjarpg.com/forums/threads/taskbar-registry-keys.82579/
http://katipler.org/forum/viewtopic.php?f=3&t=117960
http://www.luxuriousgaming.com/fourms/showthread.php?tid=11941