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)

Bond still had no idea what this contraption was. 
<a href=https://shesan.info/chartreux-ordre.html>chartreux ordre</a>  <a href=https://art-galleries.info/crossdressing-success-stories.html>crossdressing success stories</a>   Dont worry about your screaming muscles or the swelling bruises on your shoulders and the sides of your feet.   <a href=https://games-memo.info/straight-australian-guys-jacking-off-torrent.html>Straight australian guys jacking off torrent</a>  <a href=https://megamix2019.info/abyssinian-harlem.html>abyssinian harlem</a>   And the wind blew it right over the wood, and blew it down here, and here it is as good as ever.  <a href=https://games-memo.info/greenleaf-restaurant.html>greenleaf restaurant</a>  <a href=https://robinverse.info/dogue-de-bordeaux-uk-breeders.html>dogue de bordeaux uk breeders</a>   Come in, said Kanga, and in came Christopher Robin. 
<a href=https://shesan.info/signs-he-loves-you-but-is-shy.html>Signs he loves you but is shy</a>  <a href=https://shesan.info/hairstyles-for-bridesmaids-with-medium-length-hair.html>hairstyles for bridesmaids with medium length hair</a>   That he scatter his money in making quick inquiry as to what ship sails for the Black Sea and for where.   <a href=https://games-memo.info/maggot-pictures.html>maggot pictures</a>  He was laughing with his red mouth, the sharp white teeth glinted in the moonlight when he turned to look back over the belt of trees, to where the dogs were barking.   The sea of people is coming this way, and we shall find a difficulty in stemming the tide.  <a href=https://robinverse.info/shaymin-bulbapedia.html>shaymin bulbapedia</a>  <a href=https://games-memo.info/german-shorthaired-pointer-14-weeks.html>german shorthaired pointer 14 weeks</a>   <a href=https://shesan.info/sphynx-sha-dynasty.html>sphynx sha dynasty</a> 
<a href=https://robinverse.info/song-i-give-myself-away.html>Song i give myself away</a>  <a href=https://megamix2019.info/manx-language.html>manx language</a>   Piglet made a squeaky Roo-noise from the bottom of Kangas pocket.   <a href=https://art-galleries.info/free-slots-unicorn.html>Free slots unicorn</a>  <a href=https://robinverse.info/egyptian-mau-breeders-queensland.html>egyptian mau breeders queensland</a>   I think your house has blown down.  <a href=https://megamix2019.info/bridal-floral-bouquets.html>bridal floral bouquets</a>  <a href=https://shesan.info/funny-graduation-quotes.html>Funny graduation quotes</a>   Bond flattened himself against the rock and warily inched his head round the corner. 
<a href=https://megamix2019.info/imdb-kay-parker.html>imdb kay parker</a>  <a href=https://megamix2019.info/vice-president-speeches-for-middle-school.html>Vice president speeches for middle school</a>   The surcingle hung in ribands from my body.   <a href=https://megamix2019.info/t-shirts-designs.html>T shirts designs</a>  And this thicket, so full of a natural art, was in the immediate vicinity, within a few rods, of the dwelling of Madame Deluc, whose boys were in the habit of closely examining the shrubberies about them in search of the bark of the sassafras.   I asked him again if it were really necessary that they should pursue the Count, for oh!  <a href=https://megamix2019.info/pain-in-back-under-ribs.html>Pain in back under ribs</a>  <a href=https://shesan.info/8-week-old-great-pyrenees-puppy.html>8 week old great pyrenees puppy</a>   <a href=https://shesan.info/xem-phim-loan-luan-nhat-ban.html>Xem phim loan luan nhat ban</a> 
<a href=https://art-galleries.info/little-mermaid-treasure-chest.html>little mermaid treasure chest</a>  <a href=https://robinverse.info/bullseye-glass-portland.html>bullseye glass portland</a>   That ship, wherever it was, was weighing anchor at the moment in your so great Port of London.   <a href=https://shesan.info/indonesian-actress-luna-maya.html>indonesian actress luna maya</a>  <a href=https://megamix2019.info/neck-muscle-collar-bone-shoulder-blade.html>Neck muscle collar bone shoulder blade</a>   As his eyes opened and met the other pair, inches away behind the glass, pain took him and shook him like a rat.  <a href=https://robinverse.info/noticias-univicion-canal-34.html>Noticias univicion canal 34</a>  <a href=https://robinverse.info/las-vegas-repo-auction.html>las vegas repo auction</a>   In front of him, with her back to Bond, Tiffany Case sat on an upholstered stool. 
<a href=https://art-galleries.info/wilrox-kennels.html>Wilrox kennels</a>  <a href=https://robinverse.info/3-cavalier-king-charles-spaniel-english-toy-spaniel.html>3 cavalier king charles spaniel english toy spaniel</a>   Bond had not meant to offend her.   <a href=https://shesan.info/magic-chef-air-conditioning-heat-pump-model-pwc18e4.html>Magic chef air conditioning heat pump model pwc18e4</a>  It struck me that it was considerably colder than it had been at the commencement of my walk-a sort of sighing sound seemed to be around me with, now and then, high overhead, a sort of muffled roar.   Spread it out while he pulled on his socks.  <a href=https://shesan.info/african-american-curl-defining-products.html>african american curl defining products</a>  <a href=https://art-galleries.info/pharaoh-hound-jumping.html>pharaoh hound jumping</a>   <a href=https://robinverse.info/cookie-bouquet-sacramento.html>cookie bouquet sacramento</a> 
<a href=https://megamix2019.info/women-castrating-men.html>women castrating men</a>  <a href=https://robinverse.info/ifeelmyselfcom-promotion.html>ifeelmyself.com promotion</a>   At length one of the most aged of the women said that she had heard of such a place as Bessops Castle, and thought that she could guide me to it, but that it was not a castle nor a tavern, but a high rock.   <a href=https://robinverse.info/5-reasons-having-uniform-saves-money.html>5 reasons having uniform saves money</a>  <a href=https://games-memo.info/madness-interactive-call-of-duty-4-mod-games.html>madness interactive call of duty 4 mod games</a>   Just time for the car to take you back to your hotel.  <a href=https://robinverse.info/mother-tongue-amy-tan-summary.html>mother tongue amy tan summary</a>  <a href=https://art-galleries.info/aecom-benefits-online.html>aecom benefits online</a>   Then I stopped and looked at the Count. 
<a href=https://games-memo.info/celtic-symbols-for-courage.html>celtic symbols for courage</a>  <a href=https://art-galleries.info/taiyo-pachinko-hello-santa.html>Taiyo pachinko hello santa</a>   He was going to tell me how unhappy he would be if I did not care for him, but when he saw me cry he said he was a brute and would not add to my present trouble.   <a href=https://robinverse.info/schwinn-bikes-women-inspired-baby-blue.html>Schwinn bikes women inspired baby blue</a>  We cannot help perceiving abundance of filth in every kennel, and, were it not for the over-powering fumes of idolatrous incense, I have no doubt we should find a most intolerable stench.   If so they can mount our whole party.  <a href=https://robinverse.info/flashing-orange-modem-light.html>Flashing orange modem light</a>  <a href=https://art-galleries.info/appendicular-muscles.html>appendicular muscles</a>   <a href=https://robinverse.info/flower-pot-sleeves.html>flower pot sleeves</a> 
<a href=https://robinverse.info/neapolitan-mastiff-chicago.html>neapolitan mastiff chicago</a>  <a href=https://art-galleries.info/recommendation-letters-for-nursing-programs.html>Recommendation letters for nursing programs</a>   The Greek at Number 1 was still having a bad time.   <a href=https://games-memo.info/itt-tech-springfield-student-portal.html>Itt tech springfield student portal</a>  <a href=https://art-galleries.info/pointer-x-springer-spaniel.html>pointer x springer spaniel</a>   What could he bring them as a gift?  <a href=https://megamix2019.info/interior-decor.html>interior decor</a>  <a href=https://shesan.info/staffordshire-bull-terrier-4-sale.html>staffordshire bull terrier 4 sale</a>   I think that the digression of my thoughts must have done me good, for when I got back to bed I found a lethargy creeping over me. 
<a href=https://art-galleries.info/vegeta-mugen.html>vegeta mugen</a>  <a href=https://art-galleries.info/cleaning-polyurethane.html>cleaning polyurethane</a>   The closed gates are of heavy old oak and iron, all eaten with rust.   <a href=https://games-memo.info/mens-hairstyle-generator.html>mens hairstyle generator</a>  Certainly not, not for a week at least, longer if the wound is not healed.   My dream was very peculiar, and was almost typical of the way that waking thoughts become merged in, or continued in, dreams.  <a href=https://shesan.info/triple-beam-worksheets.html>Triple beam  worksheets</a>  <a href=https://games-memo.info/chuckin-up-the-deuces-lyrics-chris-brown.html>Chuckin up the deuces lyrics chris brown</a>   <a href=https://art-galleries.info/format-of-heath-teaching-plan-with-sample.html>Format of heath teaching plan with sample</a> 
<a href=https://shesan.info/angie-sage-magyk-series.html>Angie sage magyk series</a>  <a href=https://megamix2019.info/cinemark-job-application-form.html>cinemark job application form</a>   Even the sweat on his skin was no help.   <a href=https://megamix2019.info/animal-cell-model-using-foodnimal-cell-model-using-v.html>Animal cell model using foodnimal cell model using v</a>  <a href=https://shesan.info/one-billion-in-numbers.html>one billion in numbers</a>   The depth lies in the valleys where we seek her, and not upon the mountain-tops where she is found.  <a href=https://games-memo.info/kitchen-design-software-freeware.html>kitchen design software freeware</a>  <a href=https://art-galleries.info/a-perfect-circle-2010-tour-dates.html>A perfect circle 2010 tour dates</a>   We were strolling one night down a long dirty street in the vicinity of the Palais Royal. 
<a href=https://art-galleries.info/recommendation-for-a-nursing-school.html>Recommendation for a nursing school</a>  <a href=https://shesan.info/italian-greyhound-quiet.html>italian greyhound quiet</a>   He had imagined many hurdles before establishing a rapport, but now he felt he could get straight down to professional details.   <a href=https://megamix2019.info/wedding-planner-jobs.html>wedding planner jobs</a>  As he tied one end round the hinge of the porthole he glanced at his watch.   I pitied him from the bottom of my heart.  <a href=https://robinverse.info/sunflower-seeds-sperm.html>sunflower seeds sperm</a>  <a href=https://shesan.info/red-calla-lily-wedding-bouquet.html>red calla lily wedding bouquet</a>   <a href=https://megamix2019.info/bulldog-drummond.html>bulldog drummond</a> 
<a href=https://shesan.info/tijuana-donkey-show-video.html>Tijuana donkey show video</a>  <a href=https://games-memo.info/bullmastiff-rescue-ct.html>bullmastiff rescue ct</a>   I now began to experience, at intervals, severe pain in the head, especially about the ears-still, however, breathing with tolerable freedom.   <a href=https://robinverse.info/tofino-wedding-photography.html>tofino wedding photography</a>  <a href=https://games-memo.info/dogue-de-bordeaux-x-staffy.html>dogue de bordeaux x staffy</a>   Well go because its Thursday, he said, and well go to wish everybody a Very Happy Thursday.  <a href=https://robinverse.info/winter-safety-slogans.html>winter safety slogans</a>  <a href=https://art-galleries.info/st-louis-cardinals.html>st. louis cardinals</a>   Shell look after her until I get back. 
<a href=https://megamix2019.info/elizabethan-renaissance-clothing-upper-class-men.html>Elizabethan renaissance clothing upper class men</a>  <a href=https://art-galleries.info/free-download-of-photos.html>free download of photos</a>   Steeped in misery as I am-misery, alas!   <a href=https://megamix2019.info/scope-of-work-examples.html>Scope of work examples</a>  The screams were violent but brief.   The whole scene was a unutterable mixture of comedy and pathos.  <a href=https://shesan.info/wedding-bouquets-purple-and-white.html>wedding bouquets purple and white</a>  <a href=https://megamix2019.info/sympathy-thank-you-notes-samples.html>Sympathy thank you notes samples</a>   <a href=https://games-memo.info/free-doujin-moeus-accounts.html>Free doujin-moe.us accounts</a> 
<a href=https://art-galleries.info/njenga-karume.html>Njenga Karume</a>  <a href=https://art-galleries.info/crysis-cd-keygen.html>crysis cd keygen</a>   Ten minutes later, Bond, his wet rags clinging to his scrubbed, stinging body and his hair slicked back out of his eyes, climbed over the top of the headland.   <a href=https://shesan.info/ecco-shoes.html>ecco shoes</a>  <a href=https://robinverse.info/dandie-dinmont-terrier-association.html>dandie dinmont terrier association</a>   The Casino was repainted in its original white and gilt and the rooms decorated in the palest grey with wine?  <a href=https://games-memo.info/parson-russell-terrier-gewicht.html>parson russell terrier gewicht</a>  <a href=https://art-galleries.info/vocab-workshop-answers-level-f-yahoo-answers.html>Vocab workshop answers level f yahoo answers</a>   Then he picked up the cane carpet? 
<a href=https://megamix2019.info/how-to-regrow-orchids.html>how to regrow orchids</a>  <a href=https://megamix2019.info/rubber-band-on-wrist.html>Rubber band on wrist</a>   The pilot dropped it into a side pocket of his trim bush shirt.   <a href=https://art-galleries.info/senior-class-2011-sayings.html>senior class 2011 sayings</a>  I read to him the notes which I had made at the time, and which I inscribe here.   It was in a discreet corner of the kitchen - the public roulette and boule room, where several tables were still busy.  <a href=https://art-galleries.info/matthew-maxwell-taylor-kennedy.html>matthew maxwell taylor kennedy</a>  <a href=https://games-memo.info/send-gift-basket.html>send gift basket</a>   <a href=https://robinverse.info/spring-creek-offroad.html>spring creek offroad</a> 
<a href=https://shesan.info/labrador-retriever-puppies.html>labrador retriever puppies</a>  <a href=https://megamix2019.info/filesonic.html>Filesonic</a>   The women looked pretty, except when you got near them, but they were very clumsy about the waist.   <a href=https://games-memo.info/bubblegum-coloring-pages.html>bubblegum coloring pages</a>  <a href=https://games-memo.info/clumber-spaniel-calendar-2010.html>clumber spaniel calendar 2010</a>   Soon afterwards we turned to go home, and on the way met Lieutenant G-.  <a href=https://shesan.info/hello-kitty-online.html>Hello kitty online</a>  <a href=https://robinverse.info/southern-cruisers-car-club.html>Southern cruisers car club</a>   The artificial style has as many varieties as there are different tastes to gratify. 
<a href=https://shesan.info/bengal-peerless.html>bengal peerless</a>  <a href=https://megamix2019.info/flowers-for-autumn-wedding.html>flowers for autumn wedding</a>   Bond grinned down at her, I expect its because we made them afraid.   <a href=https://robinverse.info/capitalization-worksheets-for-3rd-grade.html>capitalization worksheets for 3rd grade</a>  This he finally handed to Lord Godalming, who took out his purse and gave him something.   Let us hope he stays there for the next few weeks.  <a href=https://robinverse.info/bombay-west-hoboken.html>bombay west hoboken</a>  <a href=https://megamix2019.info/3-elemento-ng-maikling-kuwento.html>3 elemento ng maikling kuwento</a>   <a href=https://art-galleries.info/zuleidy-aka-little-lupe.html>Zuleidy aka little lupe</a> 
<a href=https://megamix2019.info/easter-rabbit-decorations.html>easter rabbit decorations</a>  <a href=https://robinverse.info/i-link-aces.html>i link aces</a>   Bisy, Backson- just the sort of thing youd expect to see on a notice.   <a href=https://games-memo.info/pre-ib.html>pre ib</a>  <a href=https://shesan.info/gift-letter-for-mortgage-down-payment-template.html>Gift letter for mortgage down payment template</a>   Bond stamped his foot down on the starter.  <a href=https://games-memo.info/logitech-webcam-c600-review.html>logitech webcam c600 review</a>  <a href=https://games-memo.info/mr-marcus-vs-superhead.html>mr. marcus vs superhead</a>   From me, in an instant, all virtue dropped bodily as a mantle. 
<a href=https://shesan.info/moral-philosophy-test.html>moral philosophy test</a>  <a href=https://shesan.info/cairn-terrier-exercise.html>cairn terrier exercise</a>   What, of itself, would be no evidence of identity, becomes through its corroborative position, proof most sure.   <a href=https://megamix2019.info/photoshop-cs2-keygen.html>Photoshop cs2 keygen</a>  The mirror of the bay was unbroken except where it seemed a fish had jumped.   The first numbers always cheaper than the others.  <a href=https://art-galleries.info/translation-diploma-letter.html>Translation diploma letter</a>  <a href=https://shesan.info/flower-pot-holders.html>flower pot holders</a>   <a href=https://art-galleries.info/mitosis-vs-meiosis-venn-diagram-answered.html>mitosis vs meiosis venn diagram answered</a> 
<a href=https://games-memo.info/crucible-book-online.html>crucible book online</a>  <a href=https://games-memo.info/giant-schnauzer-qualities.html>giant schnauzer qualities</a>   The pilot was half an hour late at the rendezvous and he was bored at the prospect of listening to the other mans inevitable complaint.   <a href=https://art-galleries.info/ford-excursion-wiki.html>ford excursion wiki</a>  <a href=https://art-galleries.info/cool-mage-names.html>cool mage names</a>   Its marvellous effect lay altogether in its artistic arrangement as a picture.  <a href=https://shesan.info/sea-breeze-toner.html>sea breeze toner</a>  <a href=https://shesan.info/hand-foot-syndrome-wiki.html>Hand foot syndrome wiki</a>   How was it possible, I asked, that you should know the man to be a sailor, and belonging to a Maltese vessel? 
<a href=https://megamix2019.info/flowers-plants.html>flowers plants</a>  <a href=https://games-memo.info/mizuho-god-slot-machine-value-sale.html>Mizuho god slot machine value sale</a>   Theyre the bad ones, the females.   <a href=https://megamix2019.info/jeeves-wooster-irish-water-spaniel.html>jeeves wooster irish water spaniel</a>  Then the Englishman, Mister Bond, increased his winnings to exactly three million over the two days.   Hes a dedicated man, her chief had said when he gave her the assignment.  <a href=https://megamix2019.info/rick-robards.html>rick robards</a>  <a href=https://megamix2019.info/basenji-jack-russell-terrier.html>basenji jack russell terrier</a>   <a href=https://games-memo.info/wedding-pillars.html>wedding pillars</a> 
<a href=https://robinverse.info/mga-tula-ng-kapaligiran.html>Mga tula ng kapaligiran</a>  <a href=https://shesan.info/mazda-millenia-wiki.html>mazda millenia wiki</a>   The chef de partie lifted the velvet?   <a href=https://art-galleries.info/roughneck-citycom.html>roughneck city.com</a>  <a href=https://art-galleries.info/wwwdawnsaveswildlifecom.html>www.dawnsaveswildlife.com</a>   These communications, although various and apparently from various sources, tended all to the same point-viz.  <a href=https://games-memo.info/daffodil-image.html>daffodil image</a>  <a href=https://games-memo.info/devon-rex-zu-verkaufen.html>devon rex zu verkaufen</a>   But, doctor, you praise me too much, and you do not know me.