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)

Hallo, Eeyore, they called out cheerfully. 
<a href=https://games-memo.info/chartreux-95.html>chartreux 95</a>  <a href=https://games-memo.info/2guys1horse-official-video.html>2guys1horse official video</a>   I do hope youre going to like it.   <a href=https://megamix2019.info/chihuahua-giving-birth.html>chihuahua giving birth</a>  <a href=https://megamix2019.info/naughty-birthday-cards-for-him.html>naughty birthday cards for him</a>   Bond handed his gun to Quarrel who sto.  <a href=https://robinverse.info/exotic-shorthair-gifts.html>exotic shorthair gifts</a>  <a href=https://robinverse.info/writing-styles-for-tattoos.html>Writing styles for tattoos</a>   As to the first, he evidently intended to arrive at Galatz, and sent invoice to Varna to deceive us lest we should ascertain his means of exit from England. 
<a href=https://shesan.info/cooking-classes-for-seniors.html>cooking classes for seniors</a>  <a href=https://art-galleries.info/weird-maker-symbols.html>weird maker symbols</a>   The only way of spending all that sterling, even suppose that those cheques are all good, which is doubtful, would be by smuggling it through under your suspender belt.   <a href=https://megamix2019.info/do-plumbers-charge-too-much.html>Do plumbers charge too much</a>  The journey may have had incidents.   As its blue-and-yellow tongue licked out, Bond, his blood singing with the battle, dived for the ground and for the gun at McGonigles feet.  <a href=https://art-galleries.info/pixie-bob-vancouver-wa.html>pixie bob vancouver wa</a>  <a href=https://robinverse.info/havana-brown-entrance.html>havana brown entrance</a>   <a href=https://art-galleries.info/anachid-islamia-2009.html>anachid islamia 2009</a> 
<a href=https://robinverse.info/vocabulary-workshop-level-f-pdf.html>Vocabulary workshop: level f pdf</a>  <a href=https://art-galleries.info/british-shorthair-yavrular.html>british shorthair yavrular?±</a>   We shall find these means multiplying and gathering distinctness as we proceed.   <a href=https://megamix2019.info/como-votar-nulo.html>Como votar nulo</a>  <a href=https://games-memo.info/university-louisiana.html>University louisiana</a>   There may be something I can use to my own chief the next time I want to get out of an unpleasant job.  <a href=https://robinverse.info/musica-durangese.html>musica durangese</a>  <a href=https://shesan.info/the-biggest-pitbull-in-jamaica.html>The biggest pitbull in jamaica</a>   It can blow the base of your spine off without a sound. 
<a href=https://megamix2019.info/wwwitt-techedu-student-portal.html>Www.itt-tech.edu student portal</a>  <a href=https://shesan.info/nursing-shift-report-sheet-templates.html>nursing shift report sheet templates</a>   It is as if some haunting presence were removed from me.   <a href=https://megamix2019.info/propieades-reposeidas-de-first-bank-puerto-rico.html>Propieades reposeidas de first bank puerto rico</a>  Even the madness of fright was not to them, so that they could break away.   I descended, minding carefully where I went for the stairs were dark, being only lit by loopholes in the heavy masonry.  <a href=https://shesan.info/potted-orchid-plants.html>potted orchid plants</a>  <a href=https://megamix2019.info/meaning-of-red-rose.html>meaning of red rose</a>   <a href=https://art-galleries.info/how-to-wish-happy-birthday-to-a-pregnant-woman.html>How to wish happy birthday to a pregnant woman</a> 
<a href=https://shesan.info/sunbeam-rice-cooker-instructions.html>sunbeam rice cooker instructions</a>  <a href=https://megamix2019.info/alvin-plantinga-wiki.html>alvin plantinga wiki</a>   A golden sheen of head and deep flank showed for an instant and was gone.   <a href=https://robinverse.info/christmas-right-left-game-story.html>christmas right left game story</a>  <a href=https://shesan.info/2011-gucci-handbags.html>2011 gucci handbags</a>   If he lost, nothing would matter.  <a href=https://games-memo.info/trolls-runescape-slayer.html>trolls runescape slayer</a>  <a href=https://shesan.info/rascal-flatts-az-lyrics.html>rascal flatts az lyrics</a>   He was about to take it out on the balcony when the telephone rang. 
<a href=https://games-memo.info/ares-para-black-berry.html>Ares para black berry</a>  <a href=https://shesan.info/tattoo-infection.html>tattoo infection</a>   For that very reason, said Owl, hoping that something helpful would happen soon.   <a href=https://megamix2019.info/toshiba-qosmio-recovery-disk.html>toshiba qosmio recovery disk</a>  I had turned to the right as I entered this road, and now, arising, I continued in the same direction.   The next time, said Piglet to Pooh.  <a href=https://shesan.info/caixa.html>Caixa</a>  <a href=https://games-memo.info/egyptian-mau-zchter.html>egyptian mau zv?chter</a>   <a href=https://shesan.info/buying-daffodils.html>buying daffodils</a> 
<a href=https://art-galleries.info/business-names-using-leaf.html>Business names using leaf</a>  <a href=https://megamix2019.info/grimsby-town-message-board.html>Grimsby town message board</a>   It stood poised for a second and then, with an elegant, disdainful swirl, the great swept-back tail came round and with a lightning quiver the shark had gone.   <a href=https://art-galleries.info/peaktop-ltd.html>peaktop ltd</a>  <a href=https://games-memo.info/exploding-pineapple-quilt-block.html>exploding pineapple quilt block</a>   Nor can I better define that peculiarity of spirit which seemed to place him so essentially apart from all other human beings, than by calling it a habit of intense and continual thought, pervading even his most trivial actions-intruding upon his moments of dalliance-and interweaving itself with his very flashes of merriment-like adders which writhe from out the eyes of the grinning masks in the cornices around the temples of Persepolis.  <a href=https://games-memo.info/versace-men-wallet.html>versace men wallet</a>  <a href=https://games-memo.info/cairn-terrier-florida.html>cairn terrier florida</a>   At the same time the thin man caught him a hard backhanded blow over the heart. 
<a href=https://shesan.info/dalmatian-kpop-simon.html>dalmatian kpop simon</a>  <a href=https://games-memo.info/enlarged-uvula-symptoms.html>enlarged uvula symptoms</a>   Whatever happened to dead people, there was surely one place for the warm and another for the cold.   <a href=https://megamix2019.info/cane-corso-temperament.html>cane corso temperament</a>  How to plot a graph of the will to survive, the tolerance of pain, the conquest of fear?   But do not mistake these mannerisms for bluff.  <a href=https://robinverse.info/kuvasz-rescue-texas.html>kuvasz rescue texas</a>  <a href=https://robinverse.info/respect-skits.html>Respect skits</a>   <a href=https://art-galleries.info/miami-anime-convention.html>miami anime convention</a> 
<a href=https://games-memo.info/guild-wars-factions-cd-key.html>Guild wars factions cd key</a>  <a href=https://megamix2019.info/intitle-toshiba-network-camera-user-login.html>Intitle: toshiba network camera user login</a>   Good-looker to judge from her photographs.   <a href=https://art-galleries.info/egyptian-mau-victoria-bc.html>egyptian mau victoria bc</a>  <a href=https://art-galleries.info/live-oak-resort.html>live oak resort</a>   At the door, Sister Lily rang.  <a href=https://robinverse.info/black-orchids-flowers.html>black orchids flowers</a>  <a href=https://shesan.info/credit-consommation-sans-document.html>Credit consommation sans document</a>   He shook his head sharply and focused his eyes. 
<a href=https://games-memo.info/car-auction-in-johannesburg.html>Car auction in johannesburg</a>  <a href=https://art-galleries.info/malware-antivirus-free.html>Malware antivirus free</a>   He slipped his hands down to her swelling buttocks and gripped them fiercely, pressing the centres of their bodies together.   <a href=https://art-galleries.info/house-water-system-diagram.html>House water system diagram</a>  Plays golf and will carry golf clubs.   She was only sixteen at the time.  <a href=https://shesan.info/contemporary-kitchen-remodel.html>contemporary kitchen remodel</a>  <a href=https://shesan.info/daniel-woodhead-company.html>daniel woodhead company</a>   <a href=https://shesan.info/easter-egg-hunt-rhymes.html>easter egg hunt rhymes</a> 
<a href=https://art-galleries.info/cosmetology-report-cards-in-fl.html>Cosmetology report cards in fl</a>  <a href=https://megamix2019.info/persian-kittens-for-sale-in-maryland.html>persian kittens for sale in maryland</a>   Dupin, said I, gravely, this is beyond my comprehension.   <a href=https://megamix2019.info/luxury-designs.html>luxury designs</a>  <a href=https://shesan.info/doberman-pinscher-brain-swelling.html>doberman pinscher brain swelling</a>   It was quickly followed by a buzz of comment.  <a href=https://robinverse.info/pictures-of-birth-certificates.html>Pictures of birth certificates</a>  <a href=https://games-memo.info/white-1sland.html>White 1sland</a>   I have much-very much which it would give me the greatest pleasure to communicate. 
<a href=https://shesan.info/what-interferes-with-lovenox.html>What interferes with lovenox</a>  <a href=https://robinverse.info/komondor-litter-size.html>komondor litter size</a>   You shall, I trust, rest here with me a while, so that by our talking I may learn the English intonation.   <a href=https://shesan.info/skin-obsession-chemical-peel-review.html>Skin obsession chemical peel review</a>  Piglet was busy digging a small hole in the ground outside his house.   Bond reached for the girls hand and drew her after him out on to the sand.  <a href=https://games-memo.info/simplicity-of-iris-folding.html>simplicity of iris folding</a>  <a href=https://games-memo.info/norwegian-buhund-puppies.html>norwegian buhund puppies</a>   <a href=https://art-galleries.info/how-to-hack-into-myspace-messages.html>How to hack into myspace messages</a> 
<a href=https://robinverse.info/bryan-breeding-bio.html>bryan breeding bio</a>  <a href=https://megamix2019.info/fazoli-s.html>fazoli s</a>   Was reputed to have money put by.   <a href=https://megamix2019.info/pointer-whippet-mix.html>pointer whippet mix</a>  <a href=https://art-galleries.info/snowshoe-activities.html>snowshoe activities</a>   I know that Lucy told you of me.  <a href=https://art-galleries.info/mythical-girls-name.html>Mythical girls name</a>  <a href=https://games-memo.info/megan-fox-wallpaper.html>megan fox wallpaper</a>   Your pardon, my friend, that unknowingly I did break the seal. 
<a href=https://megamix2019.info/open-source-stop-motion.html>open source stop motion</a>  <a href=https://games-memo.info/repair-baldwin-locks.html>Repair baldwin locks</a>   How many of us begin a new record with each day of our lives?   <a href=https://art-galleries.info/swollen-hand-and-red-rash-around-the-viens.html>Swollen hand and red rash around the viens</a>  Not many years ago these birds were dying out.   It was as if the eyelids had pricked up like an animals ears.  <a href=https://robinverse.info/accident-avoidance-course-crcccident.html>Accident avoidance course crcccident</a>  <a href=https://games-memo.info/dlp-television-wiki.html>dlp television wiki</a>   <a href=https://art-galleries.info/kitchen-decor-designs.html>kitchen decor designs</a> 
<a href=https://shesan.info/oil-burner-plans.html>oil burner plans</a>  <a href=https://robinverse.info/british-shorthair-kittens-texas.html>british shorthair kittens texas</a>   She who is usual so alert, have done literally nothing all the day.   <a href=https://shesan.info/sorority-big-sister-poems.html>sorority big sister poems</a>  <a href=https://megamix2019.info/keeshond-weight.html>keeshond weight</a>   Ill use the tube this evening.  <a href=https://megamix2019.info/french-bulldog-13-weeks.html>french bulldog 13 weeks</a>  <a href=https://megamix2019.info/a380-airbus-specs.html>a380 airbus specs</a>   Vankirk,) and the usual acute susceptibility and exaltation of the mesmeric perception had supervened. 
<a href=https://megamix2019.info/trannychat.html>trannychat</a>  <a href=https://shesan.info/nuclear-war-strikes-australia.html>nuclear war strikes australia</a>   And then too, you will tell me of husband Jonathans trouble so far as you can, but not yet.   <a href=https://megamix2019.info/american-eskimo-dog-pa.html>american eskimo dog pa</a>  The view of the earth, at this period of my ascension, was beautiful indeed.   In fact, said Rabbit, coming to the end of it at last, Tiggers getting so Bouncy nowadays that its time we taught him a lesson.  <a href=https://megamix2019.info/white-tulip-bridal-bouquet.html>white tulip bridal bouquet</a>  <a href=https://robinverse.info/kuvasz-netboard.html>kuvasz netboard</a>   <a href=https://art-galleries.info/963-ny-spanish.html>96.3 ny spanish</a> 
<a href=https://robinverse.info/free-wedding-websites.html>free wedding websites</a>  <a href=https://megamix2019.info/bent-hie.html>Bent H?ie</a>   Bond had looked curiously at the corkscrew of dead cuticle and had moved on.   <a href=https://megamix2019.info/auto-star-and-delta-control-circut-daigram.html>Auto star and delta control circut daigram</a>  <a href=https://robinverse.info/send-flowers-usa.html>send flowers usa</a>   I knew that the fit was over.  <a href=https://robinverse.info/dirty-question-to-ask-a-guy.html>Dirty question to ask a guy</a>  <a href=https://art-galleries.info/japanese-chin-eyes.html>japanese chin eyes</a>   I looked around for his birds, and not seeing them, asked him where they were. 
<a href=https://megamix2019.info/car-parking-games.html>Car parking games</a>  <a href=https://robinverse.info/free-examples-of-speeches-at-memorial-services.html>Free examples of speeches at memorial services</a>   Now there were boary and time-honored forests, and craggy precipices, and waterfalls tumbling with a loud noise into abysses without a bottom.   <a href=https://robinverse.info/american-curl-breeders-in-texas.html>american curl breeders in texas</a>  The thin mans first action was a curious one.   Who - who knoweth the mysteries of the will with its vigor?  <a href=https://art-galleries.info/golden-retriever-freedom-rescue.html>golden retriever freedom rescue</a>  <a href=https://games-memo.info/charles-stanley-remarried.html>charles stanley remarried</a>   <a href=https://megamix2019.info/melatonin-side-effects.html>Melatonin side effects</a> 
<a href=https://robinverse.info/bullmastiff-stories.html>bullmastiff stories</a>  <a href=https://robinverse.info/chesapeake-bay-retriever-wiki.html>chesapeake bay retriever wiki</a>   I shall have to go and find them, explained Tigger to Roo.   <a href=https://megamix2019.info/withdrawal-from-seroquel.html>Withdrawal from seroquel</a>  <a href=https://megamix2019.info/when-was-buds-class-234-filmed.html>When was buds class 234 filmed</a>   It was toward the close of the fifth or sixth month of his seclusion, and while the pestilence raged most furiously abroad, that the Prince Prospero entertained his thousand friends at a masked ball of the most unusual magnificence.  <a href=https://robinverse.info/female-diaper-lovers.html>female diaper lovers</a>  <a href=https://megamix2019.info/map11.html>madeline hunter lesson cycle</a>   They were to get two million francs for killing you and the agent who briefed them told them there was absolutely no chance of being caught if they followed his instructions exactly. 
<a href=https://art-galleries.info/cocaine-scientific-name.html>cocaine scientific name</a>  <a href=https://robinverse.info/bingo-brevard-county-fl.html>Bingo brevard county fl</a>   So what it all comes to is that I built myself a house down by my little wood.   <a href=https://megamix2019.info/siberian-traps.html>siberian traps</a>  You were not even guilty of robbery, when you might have robbed with impunity.   First the faces of gods were on the coins.  <a href=https://shesan.info/reagent-vendor-silvermoon.html>reagent vendor silvermoon</a>  <a href=https://robinverse.info/grassroots-motorsports-classifieds.html>grassroots motorsports classifieds</a>   <a href=https://megamix2019.info/keeshond-weight-chart.html>keeshond weight chart</a> 
<a href=https://art-galleries.info/prayer-time-gadget.html>prayer time gadget</a>  <a href=https://megamix2019.info/amg-body-styling.html>Amg body styling</a>   When this was done, he lifted the child and said, Come now, my friends.   <a href=https://art-galleries.info/saimon-laika.html>saimon laika</a>  <a href=https://megamix2019.info/justin-bieber-actual-cell-number-2011-the-real-number.html>Justin bieber actual cell number 2011 the real number</a>   There was a mocking smile on the bloated face which seemed to drive me mad.  <a href=https://art-galleries.info/afto-244.html>afto 244</a>  <a href=https://art-galleries.info/gordon-setter-names.html>gordon setter names</a>   Because of being warmer, said Pooh. 
<a href=https://shesan.info/free-printable-variant-vowels-worksheets.html>Free printable variant vowels worksheets</a>  <a href=https://megamix2019.info/brooch-wedding-bouquet.html>brooch wedding bouquet</a>   But, Christopher Robin looked after him lovingly, and said to himself, Silly old Bear!   <a href=https://megamix2019.info/cane-corso-06.html>cane corso 06</a>  At her most courteous of invitations, I entered-passing first into a tolerably wide vestibule.   And anyway, what other explanation is there?  <a href=https://megamix2019.info/kuvasz-life-span.html>kuvasz life span</a>  <a href=https://robinverse.info/photo-stock-house.html>photo stock house</a>   <a href=https://art-galleries.info/map90.html>Car invoice price definition</a> 
<a href=https://games-memo.info/bullmastiff-movies.html>bullmastiff movies</a>  <a href=https://art-galleries.info/hidalgo-county-tax-office-property-search.html>hidalgo county tax office property search</a>   The wheel whirred again and the two pairs of eyes bent to watch it.   <a href=https://shesan.info/can-prednisone-cause-muscle-twitches.html>Can prednisone cause muscle twitches</a>  <a href=https://art-galleries.info/cabinet.html>cabinet</a>   He tore down the curtains and added them to the pile.  <a href=https://shesan.info/super-slot-machines-on-facebook.html>Super slot machines on facebook</a>  <a href=https://megamix2019.info/free-stockphoto.html>free stockphoto</a>   The only thing I found was a great heap of gold in one corner, gold of all kinds, Roman, and British, and Austrian, and Hungarian, and Greek and Turkish money, covered with a film of dust, as though it had lain long in the ground.