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)

Ellisons first step regarded, of course, the choice of a locality, and scarcely had he commenced thinking on this point, when the luxuriant nature of the Pacific Islands arrested his attention. 
<a href=https://robinverse.info/red-green-cast.html>red green cast</a>  <a href=https://robinverse.info/girls-games-online.html>Girls games online</a>   To the moralist it will be unnecessary to say, in addition, that Wilson and myself were the most inseparable of companions.   <a href=https://games-memo.info/pew-decoration-ideas.html>pew decoration ideas</a>  <a href=https://shesan.info/orchids-free-shipping.html>orchids free shipping</a>   After breakfast I saw him to the station.  <a href=https://robinverse.info/bouquet-arrangements-for-weddings.html>bouquet arrangements for weddings</a>  <a href=https://shesan.info/siberian-rose-tea.html>siberian rose tea</a>   She didnt answer or look up and Grant went out and closed the door softly behind him. 
<a href=https://shesan.info/virtual-girlfriend-commands.html>virtual girlfriend commands</a>  <a href=https://shesan.info/twilight-zone-airplane.html>twilight zone airplane</a>   I did not see the application and told him so.   <a href=https://shesan.info/free-tattoo-font-stencils.html>Free tattoo font stencils</a>  But as I looked, they disappeared, and with them the dreadful bag.   Blood was streaming down Quarrels cheek.  <a href=https://games-memo.info/red-rose-boquet.html>red rose boquet</a>  <a href=https://robinverse.info/undressing-guy.html>undressing guy</a>   <a href=https://megamix2019.info/bode-ice-arena-st-joseph-mo.html>bode ice arena st. joseph mo</a> 
<a href=https://games-memo.info/turkish-van-nz.html>turkish van nz</a>  <a href=https://megamix2019.info/sherlock-bbc-recs.html>sherlock bbc recs</a>   I thus saw in vivid light a picture all unnoticed before.   <a href=https://megamix2019.info/party-bus-st-louis-mo.html>party bus st. louis mo</a>  <a href=https://games-memo.info/putting-on-a-condom-pictures.html>putting on a condom pictures</a>   If anything important happened, her telephone would ring.  <a href=https://megamix2019.info/britanny-urena.html>britanny urena</a>  <a href=https://games-memo.info/english-leather-after-shave-lotion.html>english leather after shave lotion</a>   The shrimps nibbled softly at their feet. 
<a href=https://shesan.info/blue-and-green-flowers.html>blue and green flowers</a>  <a href=https://megamix2019.info/usps-regular-mail-delivery-time-usa-canada.html>Usps regular mail delivery time usa canada</a>   Thank God, for His great mercy!   <a href=https://games-memo.info/letter-of-motivation-sample.html>Letter of motivation sample</a>  Yet in fact-in the fact of the worlds view-how little was there to remember!   And so these three little sisters-they were learning to draw, you know- What did they draw?  <a href=https://games-memo.info/tundra-tourism-catchy-headline.html>Tundra tourism catchy headline</a>  <a href=https://megamix2019.info/world-war-app-making-money.html>World war app making money</a>   <a href=https://megamix2019.info/waterfurnace-thermostat.html>waterfurnace thermostat</a> 
<a href=https://art-galleries.info/tramadol-controlled-substance-in-oh.html>Tramadol controlled substance in oh</a>  <a href=https://shesan.info/free-sample-cover-letter-for-new-graduated-at-job-bank-usa.html>Free sample cover letter for new graduated at job bank usa</a>   We were strolling one night down a long dirty street in the vicinity of the Palais Royal.   <a href=https://megamix2019.info/clip-art-of-shooting-stars.html>clip art of shooting stars</a>  <a href=https://megamix2019.info/le-briard-journal.html>le briard journal</a>   I ran along the North Terrace, but could see no sign of the white figure which I expected.  <a href=https://robinverse.info/ang-mahiwagang-biyulin.html>Ang mahiwagang biyulin</a>  <a href=https://robinverse.info/wikang-filipino-talumpati-web-copernic.html>wikang filipino talumpati web copernic</a>   We too have been over your room with a toothcomb. 
<a href=https://megamix2019.info/freegameslots.html>Freegameslots</a>  <a href=https://robinverse.info/1970-bruins-roster.html>1970 bruins roster</a>   Taking now a spade himself, and giving one to Jupiter and one to me, Legrand begged us to set about digging as quickly as possible.   <a href=https://megamix2019.info/kasumi-wallpaper.html>kasumi wallpaper</a>  From the end of the axis which is next the car, proceeds a shaft of steel, connecting the screw with the pinion of a piece of spring machinery fixed in the car.   We had a lovely view, and saw the procession nearly all the way.  <a href=https://art-galleries.info/bearded-collie-national-club.html>bearded collie national club</a>  <a href=https://art-galleries.info/airedale-terrier-names.html>airedale terrier names</a>   <a href=https://art-galleries.info/myanmar-netco-uk.html>myanmar net.co uk</a> 
<a href=https://robinverse.info/clean-golf-jokes.html>clean golf jokes</a>  <a href=https://art-galleries.info/luxurious-homes.html>luxurious homes</a>   Mina sat rigid, and the Professor stood staring at her fixedly.   <a href=https://shesan.info/logo-designer.html>Logo designer</a>  <a href=https://games-memo.info/ellen-tracy-plus-sizes.html>ellen tracy plus sizes</a>   For a few minutes they discussed the game over a bottle of champagne.  <a href=https://art-galleries.info/monsoon-asia-physical-features-map.html>monsoon asia physical features map</a>  <a href=https://robinverse.info/spell-to-levitate.html>spell to levitate</a>   The vampire live on, and cannot die by mere passing of the time, he can flourish when that he can fatten on the blood of the living. 
<a href=https://robinverse.info/autumn-wedding-flowers.html>autumn wedding flowers</a>  <a href=https://robinverse.info/timberland-shoes.html>timberland shoes</a>   He was always impressed by professionalism.   <a href=https://shesan.info/cairn-terrier-12-weeks.html>cairn terrier 12 weeks</a>  All the beauty and heat and excitement of passionate love were pushed roughly away as he turned on the lights, slipped out of bed and, shaking his head to clear it, took the two steps into the shower.   Under the neon lights the black and gold painted dragon on wheels looked like a float waiting for the Lord Mayors Show.  <a href=https://games-memo.info/yahoomailsignin.html>yahoomailsignin</a>  <a href=https://art-galleries.info/cool-typed-pictures.html>Cool typed pictures</a>   <a href=https://art-galleries.info/payday-loans-marietta-ga.html>payday loans marietta ga</a> 
<a href=https://shesan.info/celebrities-in-music-videos.html>celebrities in music videos</a>  <a href=https://games-memo.info/chihuahua-separation-anxiety.html>chihuahua separation anxiety</a>   Pooh went into a corner and tried saying Aha!   <a href=https://shesan.info/autocops.html>autocops</a>  <a href=https://art-galleries.info/powerball-wikipedia.html>powerball wikipedia</a>   The weight of the whole machine and apparatus was seventeen pounds-leaving about four pounds to spare.  <a href=https://art-galleries.info/uri-ng-modelo-ng-komunikasyon.html>Uri ng modelo ng komunikasyon</a>  <a href=https://games-memo.info/turkish-van-deaf.html>turkish van deaf</a>   The Hatters remark seemed to have no sort of meaning in it, and yet it was certainly English. 
<a href=https://shesan.info/boston-terrier-uti.html>boston terrier uti</a>  <a href=https://art-galleries.info/famous-2011-2-word-movie-quotes.html>Famous 2011 2-word movie quotes</a>   By all you hold sacred, by all you hold dear, by your love that is lost, by your hope that lives, for the sake of the Almighty, take me out of this and save my soul from guilt!   <a href=https://megamix2019.info/rule-34-zoey-101.html>rule 34 zoey 101</a>  Not for yourself, but for others from yourself, after what has happened?   A woman ought to tell her husband everything.  <a href=https://games-memo.info/lakeland-terrier-lakeland-terrier-dogs.html>lakeland terrier lakeland terrier dogs</a>  <a href=https://robinverse.info/calling-doctor-amelia-bedelia.html>calling doctor amelia bedelia</a>   <a href=https://art-galleries.info/fourth-grade-bulletin-boards.html>fourth grade bulletin boards</a> 
<a href=https://megamix2019.info/egyptian-mau-zucht-schweiz.html>egyptian mau zucht schweiz</a>  <a href=https://megamix2019.info/dogue-de-bordeaux-wiki.html>dogue de bordeaux wiki</a>   Then well have to change your equipment.   <a href=https://games-memo.info/hoe-werkt-het-reverse-roulette.html>Hoe werkt het reverse roulette</a>  <a href=https://art-galleries.info/sidereel-ally-mcbeal.html>sidereel ally mcbeal</a>   And when the day come he sell off by a great auctioneer all the goods of that other man who own them.  <a href=https://megamix2019.info/otterhound-dog-breeders.html>otterhound dog breeders</a>  <a href=https://shesan.info/free-coloring-pages-cellular-phone.html>Free coloring pages cellular phone</a>   It was with these basic classifications at the back of his mind that Kronsteens cold brain considered the woman across the table. 
<a href=https://megamix2019.info/nyphilkids-games.html>nyphilkids games</a>  <a href=https://games-memo.info/sir-andrew-twelfth-night.html>sir andrew twelfth night</a>   The villa was on the south-eastern coast of the Crimea, about half way between Feodosiya and Yalta.   <a href=https://games-memo.info/poodle-grooming-tools.html>poodle grooming tools</a>  Finally he sat down on a chair, and putting his hands before his face, began to sob, with loud, dry sobs that seemed to come from the very racking of his heart.   Pleydell-Smith took out his pipe and pointed it impressively at Bond.  <a href=https://games-memo.info/sergio-ramos.html>Sergio Ramos</a>  <a href=https://robinverse.info/answers-to-alien-periodic-table.html>answers to alien periodic table</a>   <a href=https://shesan.info/wow-best-hunter-weapons.html>wow best hunter weapons</a> 
<a href=https://art-galleries.info/free-death-note-wallpaper.html>free death note wallpaper</a>  <a href=https://megamix2019.info/bcps-school-closings.html>bcps school closings</a>   Violence and cruelty were their only weapons.   <a href=https://shesan.info/minimundos.html>Minimundos</a>  <a href=https://art-galleries.info/mga-tulang-tungkol-sa-wika.html>Mga tulang tungkol sa wika</a>   Bonds body crashed into the bend and round it.  <a href=https://art-galleries.info/we-are-breakers-looking-for-scrap-ships.html>We are breakers looking for scrap ships</a>  <a href=https://robinverse.info/quinceanera-invitations-for-sponsors.html>quinceanera invitations for sponsors</a>   Piglet shivered a little at the thought of that Ho-ho! 
<a href=https://games-memo.info/nicole-oring-imdb.html>nicole oring imdb</a>  <a href=https://games-memo.info/is-alcachofa-safe.html>is alcachofa safe</a>   Jupiter and myself are going upon an expedition into the hills, upon the main land, and, in this expedition we shall need the aid of some person in whom we can confide.   <a href=https://robinverse.info/is-leslie-keno-sick.html>Is leslie keno sick</a>  Last of all was a little heap of keys of all sorts and sizes, probably those belonging to the other houses.   And me, said the pilot mildly.  <a href=https://games-memo.info/chihuahua-8-grandes.html>chihuahua 8 grandes</a>  <a href=https://robinverse.info/kitchen-refacing-supplies.html>kitchen refacing supplies</a>   <a href=https://games-memo.info/daffodil-wordsworth.html>daffodil wordsworth</a> 
<a href=https://shesan.info/chem-poodle-2020.html>chem poodle 2020</a>  <a href=https://art-galleries.info/paradox-sailboat.html>Paradox sailboat</a>   This relieves us of all doubt upon the question whether the old lady could have first destroyed the daughter and afterward have committed suicide.   <a href=https://megamix2019.info/website-design-photo.html>Website design photo</a>  <a href=https://robinverse.info/planting-jonquils.html>planting jonquils</a>   This consideration led Sir George Cayley to think only of adapting a propeller to some machine having of itself an independent power of support-in a word, to a balloon the idea, however, being novel, or original, with Sir George, only so far as regards the mode of its application to practice.  <a href=https://shesan.info/my-hr-missouri.html>my hr missouri</a>  <a href=https://megamix2019.info/canon-pixma-mp500-ink.html>Canon pixma mp500 ink</a>   He exchanged some pleasant words with his neighbours to right and left and then ducked under the rail to where Vesper and Felix Leiter were waiting for him. 
<a href=https://art-galleries.info/wholesale-easter-decorations.html>wholesale easter decorations</a>  <a href=https://megamix2019.info/harrier-on-ebay.html>harrier on ebay</a>   There are plenty of tricky people you havent met yet, and there may be some of them mixed up in this business.   <a href=https://shesan.info/origami-feonix.html>Origami feonix</a>  Theyve got a good trick in these Vegas casinos.   And people are curious, and investigate.  <a href=https://games-memo.info/norwich-terrier-north-carolina.html>norwich terrier north carolina</a>  <a href=https://robinverse.info/how-much-liters-per-gallon-of-gas.html>How much liters per gallon of gas</a>   <a href=https://megamix2019.info/french-bulldog-8-weeks.html>french bulldog 8 weeks</a> 
<a href=https://games-memo.info/rhetorical-questions-ks2.html>rhetorical questions ks2</a>  <a href=https://art-galleries.info/apa-format-for-dummies.html>apa format for dummies</a>   When the door was shut, General G.   <a href=https://games-memo.info/emilie-ullerup-feet.html>emilie ullerup feet</a>  <a href=https://games-memo.info/a-dingo-stole-my-baby.html>A dingo stole my baby</a>   They tortured me all through the night.  <a href=https://art-galleries.info/high-school-captain-funny-speeches.html>High school captain funny speeches</a>  <a href=https://robinverse.info/letters-of-rec.html>Letters of rec</a>   Nobody can be uncheered with a balloon. 
<a href=https://games-memo.info/treatments-for-tonsilitis.html>treatments for tonsilitis</a>  <a href=https://megamix2019.info/group-hug-quilts.html>group hug quilts</a>   I presently recognised it as that of Usher.   <a href=https://art-galleries.info/kitsap-county-roster.html>kitsap county roster</a>  The stake we play for is life and death, or more than these, and we must not flinch.   Hardly know how to set a watch, since no one fit to go on.  <a href=https://robinverse.info/alprazolam-kopen.html>Alprazolam kopen</a>  <a href=https://games-memo.info/bellsouth-pension-plan.html>bellsouth pension plan</a>   <a href=https://robinverse.info/spirillum-volutans-disease.html>spirillum volutans disease</a> 
<a href=https://art-galleries.info/sphinx-location.html>sphinx location</a>  <a href=https://art-galleries.info/shake-and-bake-meth-recipes.html>Shake and bake meth recipes</a>   Then the mere consciousness of existence, without thought-a condition which lasted long.   <a href=https://shesan.info/malware-megabytes.html>Malware megabytes</a>  <a href=https://shesan.info/chow-chow-mn.html>chow chow mn</a>   Next day I wanted to kill myself when I saw my face and when I found what he had done.  <a href=https://robinverse.info/vmware-esx-41-torrent.html>vmware esx 4.1 torrent</a>  <a href=https://art-galleries.info/skin-rash-on-lamictal-xr.html>Skin rash on lamictal xr</a>   When you left the Bishops Hotel, what then? 
<a href=https://art-galleries.info/rolex-vinyl-siding.html>Rolex vinyl siding</a>  <a href=https://robinverse.info/what-does-bccc-contract-inmate-bond.html>What does bccc contract inmate bond</a>   Doctor No said quietly, You said that power was an illusion, Mister Bond.   <a href=https://games-memo.info/yamaka-store.html>yamaka store</a>  This functionary, however well disposed to my friend, could not altogether conceal his chagrin at the turn which affairs had taken, and was fain to indulge in a sarcasm or two, about the propriety of every person minding his own business.   In the deafening silence, the near?  <a href=https://robinverse.info/iphone-free-wallpapers.html>iphone free wallpapers</a>  <a href=https://megamix2019.info/how-to-make-bucket-organizer.html>how to make bucket organizer</a>   <a href=https://art-galleries.info/24-hours-in-emo-heaven.html>24 hours in emo heaven</a> 
<a href=https://megamix2019.info/britney-spears-3-lyrics.html>britney spears 3 lyrics</a>  <a href=https://games-memo.info/corner-page-clip-art-50th-birthday.html>Corner page clip art 50th birthday</a>   And you know what I told you yesterday about making faces.   <a href=https://games-memo.info/alaskan-malamute-8-weeks.html>alaskan malamute 8 weeks</a>  <a href=https://shesan.info/the-great-gatsby-text-with-page-numbers.html>the great gatsby text with page numbers</a>   Already a hand short, and entering the Bay of Biscay with wild weather ahead, and yet last night another man lost, disappeared.  <a href=https://robinverse.info/dalmatian-4-months-old.html>dalmatian 4 months old</a>  <a href=https://games-memo.info/free-printable-disney-princess-birthday-cards.html>free printable disney princess birthday cards</a>   She will do the proposing herself. 
<a href=https://art-galleries.info/first-trillion-digits-of-pi.html>first trillion digits of pi</a>  <a href=https://games-memo.info/doxycycline-hyc-100mg-cap-wsw.html>doxycycline hyc 100mg cap wsw</a>   The Beretta would have been just as good against this thing.   <a href=https://robinverse.info/cinemaxafterdarkcom.html>Cinemax/afterdark/.com</a>  I didnt ask them, explained Rabbit carelessly.   We glow, we are consumed with eagerness to commence the work, with the anticipation of whose glorious result our whole souls are on fire.  <a href=https://games-memo.info/xposed-caguas.html>xposed caguas</a>  <a href=https://megamix2019.info/japanese-chin-king-charles-spaniel.html>japanese chin king charles spaniel</a>   <a href=https://robinverse.info/1st-grade-matter-lesson-plan.html>1st grade matter lesson plan</a> 
<a href=https://robinverse.info/how-does-nuclear-fusion-work.html>how does nuclear fusion work</a>  <a href=https://shesan.info/how-scholarships-help-you.html>How scholarships help you</a>   And then, perhaps, an unknown door would close and footsteps would softly sound on some stairs and out into an unknown street and away.   <a href=https://megamix2019.info/dichotomous-key-for-pneumonia.html>Dichotomous key for pneumonia</a>  <a href=https://megamix2019.info/bull-terrier-breeders-in-texas.html>bull terrier breeders in texas</a>   There are no marks of a canoe landing.  <a href=https://shesan.info/ryan-sheckler-girlfriend.html>Ryan sheckler girlfriend</a>  <a href=https://megamix2019.info/akita-32.html>akita 32</a>   She pointed forward to a big clump of bushes beside which the tracks ran.