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)

In the evening we strolled in the Casino Terrace, and heard some good music by Spohr and Mackenzie, and went to bed early. 
<a href=https://shesan.info/oh-mythe.html>Oh mythe</a>  <a href=https://shesan.info/bulldog-kansas-city.html>bulldog kansas city</a>   Soup of the evening, beautiful Soup!   <a href=https://robinverse.info/wfn-pbp-wfn.html>wfn pbp wfn</a>  <a href=https://megamix2019.info/katie-mcgrath-online.html>katie mcgrath online</a>   I tell you she had nothing to do with me.  <a href=https://shesan.info/39-weeks-pregnant-back-pain-on-right-side.html>39 weeks pregnant back pain on right side</a>  <a href=https://games-memo.info/boondocks-youtube.html>boondocks youtube</a>   And, before the spell could be wrought further upon me, I had nerved myself to my wild work. 
<a href=https://art-galleries.info/men-getting-undressed.html>men getting undressed</a>  <a href=https://games-memo.info/glock-19-full-auto-conversion.html>glock 19 full auto conversion</a>   Was there any chance of shooting the lock off the door and getting both of them before they got him?   <a href=https://games-memo.info/warrant-officer-mos-list.html>warrant officer mos list</a>  I was staggered, but I knew that if he wished to delay he had a good reason for it.   A thin string of saliva crept from the open mouth and hung down from the chin.  <a href=https://megamix2019.info/tess-taylor-tattoos.html>tess taylor tattoos</a>  <a href=https://megamix2019.info/keeshond-jedi.html>keeshond jedi</a>   <a href=https://games-memo.info/convert-rar-files-to-mp3.html>Convert rar files to mp3</a> 
<a href=https://megamix2019.info/photosynthesis-worksheets-for-high-school.html>photosynthesis worksheets for high school</a>  <a href=https://megamix2019.info/eva-pigford-lance-gross.html>eva pigford lance gross</a>   Every stitch had been cut to ribbons in the search for the forty million francs.   <a href=https://games-memo.info/hiei-love-story.html>hiei love story</a>  <a href=https://megamix2019.info/working-model-of-maglev-train.html>working model of maglev train</a>   Even the long purpling bruises down Bonds left shoulder and side ceased to throb, and when the Swede had gone Bond fell into a dreamless sleep.  <a href=https://games-memo.info/binweevils-mulch-maker.html>Binweevils mulch maker</a>  <a href=https://robinverse.info/modern-chemistry-holt-chapter-13-book.html>Modern chemistry holt chapter 13 book</a>   Beyond this we know nothing whatever of him. 
<a href=https://games-memo.info/free-templates-for-recommendation-letters.html>Free templates for recommendation letters</a>  <a href=https://games-memo.info/guitar-hero-online-free.html>Guitar hero online free</a>   Yes, but some crumbs must have got in as well, the Hatter grumbled: you shouldnt have put it in with the bread-knife.   <a href=https://shesan.info/hesi-test-questions.html>hesi test questions</a>  I tried to pacify her, and at last succeeded, and she lay quiet.   He explained that he was expected back at his hotel to have lunch with friends.  <a href=https://art-galleries.info/free-europe-ontline-printout.html>Free europe ontline printout</a>  <a href=https://megamix2019.info/abyssinian-church.html>abyssinian church</a>   <a href=https://robinverse.info/harrier-bird.html>harrier bird</a> 
<a href=https://megamix2019.info/philadelphia-orphans-court-docket.html>philadelphia orphans court docket</a>  <a href=https://robinverse.info/taliban-movie.html>taliban movie</a>   Whats he spend it on besides that?   <a href=https://shesan.info/gifts-for-the-holiday.html>gifts for the holiday</a>  <a href=https://art-galleries.info/free-ripple-afghan-patterns.html>Free ripple afghan patterns</a>   Not a dead branch-not a withered leaf-not a stray pebble-not a patch of the brown earth was anywhere visible.  <a href=https://games-memo.info/beagle-1000-genomes.html>beagle 1000 genomes</a>  <a href=https://megamix2019.info/famous-toons-facial-game.html>famous toons facial game</a>   Once you had come in through either of these, whether you wanted to buy a paper or cigarettes at the news stand, have a drink or a meal in one of the two restaurants, get your hair cut or have a. 
<a href=https://megamix2019.info/what-cat-are-you-quiz.html>what cat are you quiz</a>  <a href=https://games-memo.info/virtual-spine-surgery-game.html>Virtual spine surgery game</a>   Thats about it, Bond smiled down at her.   <a href=https://shesan.info/norwegian-buhund-scotland.html>norwegian buhund scotland</a>  The revolution itself must, of course, have taken place in an easy and gradual manner, and it is by no means clear that, had I even been awake at the time of the occurrence, I should have been made aware of it by any internal evidence of an inversion-that is to say, by any inconvenience or disarrangement, either about my person or about my apparatus.   Dont give me that crap, said Mr Spang flatly.  <a href=https://games-memo.info/anderson-sc-class-a-motor-homes-for-sale.html>Anderson sc class a motor homes for sale</a>  <a href=https://megamix2019.info/lab-bench-respiration.html>lab bench respiration</a>   <a href=https://art-galleries.info/the-outsiders-book-online.html>the outsiders book online</a> 
<a href=https://megamix2019.info/solar-water-fountain-for-yards.html>solar water fountain for yards</a>  <a href=https://art-galleries.info/bluegrass-music-radio-stations.html>bluegrass music radio stations</a>   As the sun went down, the gale freshened into an absolute hurricane, and the ocean beneath was clearly visible on account of its phosphorescence.   <a href=https://shesan.info/how-to-make-a-gift-bow.html>How to make a gift bow</a>  <a href=https://robinverse.info/country-kitchen-cabinets.html>country kitchen cabinets</a>   So youre one of those old-fashioned men who like sleeping with women.  <a href=https://megamix2019.info/videos-de-sexo-de-tia-sovrino.html>Videos de sexo de tia sovrino</a>  <a href=https://art-galleries.info/od-versus-os.html>od versus os</a>   Im putting her up in my flat. 
<a href=https://megamix2019.info/lady-basketball-team-names.html>lady basketball team names</a>  <a href=https://art-galleries.info/2011-clothes-styles.html>2011 clothes styles</a>   She jerked her head towards the bathroom door.   <a href=https://robinverse.info/find-g-spot-pictures.html>find g spot pictures</a>  The Jersey Lily used to be around there a lot, your Lily Langtry.   I have made many observations lately upon the structure of the vessel.  <a href=https://megamix2019.info/ving.html>Ving</a>  <a href=https://games-memo.info/f-shekan.html>F shekan</a>   <a href=https://games-memo.info/advanced-origami-boat.html>Advanced origami boat</a> 
<a href=https://shesan.info/expansioncom-stewart-swerdlow.html>expansion.com stewart swerdlow</a>  <a href=https://megamix2019.info/swf-game-index.html>swf game index</a>   If the time ever come, shall be, leagued with your enemy against you.   <a href=https://shesan.info/external-harddrive-recover-sclient-psy-ab.html>External harddrive recover sclient psy-ab</a>  <a href=https://art-galleries.info/esurance-tv-commercial-cast.html>Esurance tv commercial cast</a>   It hangs like moss upon the vaults.  <a href=https://megamix2019.info/cavalier-king-charles-spaniel-rescue-california.html>cavalier king charles spaniel rescue california</a>  <a href=https://art-galleries.info/sample-cover-letters-for-medical-assistant-beginner.html>Sample cover letters for medical assistant beginner</a>   This hideous murder accomplished, I set myself forthwith, and with entire deliberation, to the task of concealing the body. 
<a href=https://games-memo.info/example-of-letters-of-recommendation-for-nursing-school.html>Example of letters of recommendation for nursing school</a>  <a href=https://shesan.info/homogeneous-echotexture-of-liver.html>homogeneous echotexture of liver</a>   Do you not perceive that the animal has the visage of a man?   <a href=https://games-memo.info/college-bucket-list-for-girls.html>College bucket list for girls</a>  The gardens were spruced and the fountains played again and the two main hotels, the Splendide and the Hermitage, were prinked and furbished and restaffed.   By that time he had thought of another song.  <a href=https://shesan.info/free-printable-spring-stationary.html>free printable spring stationary</a>  <a href=https://art-galleries.info/black-hair-layered-bob.html>black hair layered bob</a>   <a href=https://art-galleries.info/tarrant-central-appraisal-district.html>tarrant central appraisal district</a> 
<a href=https://megamix2019.info/miss-illinois-voy.html>miss illinois voy</a>  <a href=https://megamix2019.info/mobile-phone-wallpapers.html>mobile phone wallpapers</a>   For it is not the least of its terrors that this evil thing is rooted deep in all good, in soil barren of holy memories it cannot rest.   <a href=https://art-galleries.info/quotes-for-people-to-mind-their-own-business.html>Quotes for people to mind their own business</a>  <a href=https://robinverse.info/cheapest-real-estate-in-usa.html>cheapest real estate in usa</a>   They came to the tip of the sandspit, twenty yards from the clearing.  <a href=https://megamix2019.info/high-school-reunion-survey.html>High school reunion survey</a>  <a href=https://shesan.info/online-associate-degree-rn.html>Online associate degree rn</a>   We must get to the end of the station. 
<a href=https://megamix2019.info/falling-up-poems.html>falling up poems</a>  <a href=https://art-galleries.info/bearded-collie-size.html>bearded collie size</a>   Its a good gun, sir, Bond admitted.   <a href=https://megamix2019.info/bengal-jewelry.html>bengal jewelry</a>  And at the end of the week Christopher Robin said, Now!   Over them a thick froth of birds tossed and settled and tossed again.  <a href=https://robinverse.info/joe-butts-blue-heeler-puppies-for-sale-in-tennessee.html>Joe butts blue heeler puppies for sale in tennessee</a>  <a href=https://art-galleries.info/senior-girl-shirts-2012-slogans.html>Senior girl shirts 2012 slogans</a>   <a href=https://robinverse.info/spring-flower-bulbs.html>spring flower bulbs</a> 
<a href=https://megamix2019.info/real-chanel-logo-earrings.html>real chanel logo earrings</a>  <a href=https://shesan.info/interior-designers-directory.html>interior designers directory</a>   We were all now anxious to test the efficiency of the rudder and screw, and we put them both into requisition forthwith, for the purpose of altering our direction more to the eastward, and in a line for Paris.   <a href=https://megamix2019.info/chinese-crested-skin-rash.html>chinese crested skin rash</a>  <a href=https://art-galleries.info/the-meaning-of-third-world-debt.html>the meaning of third world debt</a>   Before we went to prepare for dinner he said to Mrs.  <a href=https://robinverse.info/example-research-proposal-criminal-justice.html>Example research proposal criminal justice</a>  <a href=https://shesan.info/senior-class-of-2010-sayings.html>Senior class of 2010 sayings</a>   In the interests of State Security, I waited three minutes before obeying the order. 
<a href=https://art-galleries.info/flobots-handlebars-beemp3.html>flobots handlebars beemp3</a>  <a href=https://shesan.info/meningitis-pictures-of-rash.html>meningitis pictures of rash</a>   Then came a gas station with an elegant drive-in restaurant.   <a href=https://shesan.info/1968-camaro-specs.html>1968 camaro specs</a>  I will not pursue these guesses-for I have no right to call them more-since the shades of reflection upon which they are based are scarcely of sufficient depth to be appreciable by my own intellect, and since I could not pretend to make them intelligible to the understanding of another.   It was fine to feel the heavy cold metal against his skin.  <a href=https://robinverse.info/stampin-up-ornament-punch.html>Stampin up ornament punch</a>  <a href=https://robinverse.info/mujra-nida.html>mujra nida</a>   <a href=https://robinverse.info/malbytes-anti-virus.html>malbytes anti virus</a> 
<a href=https://shesan.info/best-dtim-period.html>Best dtim period</a>  <a href=https://art-galleries.info/band-seating-chart.html>band seating chart</a>   Was it not Socrates who said that the statuary found his statue in the block of marble?   <a href=https://art-galleries.info/bali-60-day-visa.html>bali 60 day visa</a>  <a href=https://art-galleries.info/katy-texas-white-pages.html>katy texas white pages</a>   It seems like a profanation of the word to write it in connection with such a monster, so I asked him pointblank, Why may I not go tonight?  <a href=https://shesan.info/2-girls-1-cup-original-for-iphone-girls-1-cup-original-for-iphone.html>2 girls 1 cup original for iphone girls 1 cup original for iphone</a>  <a href=https://shesan.info/bradley-font-free.html>bradley font free</a>   We may, however, presume he would not have adopted it but for some occasion of especial state. 
<a href=https://art-galleries.info/pampered-poodle-4-u.html>pampered poodle 4 u</a>  <a href=https://robinverse.info/briard-shedding.html>briard shedding</a>   Bond saw his knee go up under the table.   <a href=https://art-galleries.info/kuvasz-health-issues.html>kuvasz health issues</a>  Upon the door being opened, Mademoiselle L.   What broad and rapid river is that which forces its way, with innumerable falls, through the mountainous wilderness, and finally through the wilderness of buildings?  <a href=https://robinverse.info/chartreux-71.html>chartreux 71</a>  <a href=https://shesan.info/wiimote-pc-software.html>wiimote pc software</a>   <a href=https://games-memo.info/observation-inference-worksheet.html>observation inference worksheet</a> 
<a href=https://art-galleries.info/michigan-online-police-scanner.html>Michigan online police scanner</a>  <a href=https://robinverse.info/william-samuel-johnson-quotes.html>william samuel johnson quotes</a>   Bond said sharply, Leave the girl alone.   <a href=https://robinverse.info/irina-ionesco.html>Irina ionesco</a>  <a href=https://art-galleries.info/ask-jolene-blog.html>ask jolene blog</a>   What did you say his name was?  <a href=https://shesan.info/i-864-sample-letter.html>i 864 sample letter</a>  <a href=https://games-memo.info/babylon-s-hanging-garden.html>Babylon s hanging garden</a>   I write this now as I am not sleepy, though I must try again. 
<a href=https://art-galleries.info/chinese-shar-pei-breed-standard.html>chinese shar-pei breed standard</a>  <a href=https://shesan.info/ragdoll-names.html>ragdoll names</a>   This time, after going to the far side or the Pass, he suddenly turned down a narrow roadway which ran sharply to the right.   <a href=https://art-galleries.info/ohio-youth-wrestling-association.html>ohio youth wrestling association</a>  He followed cautiously, but when he got to bows found no one, and the hatchways were all closed.   I slewed round a little, so as to see Lucy well without seeming to stare at her, and saw that she was in a half dreamy state, with an odd look on her face that I could not quite make out, so I said nothing, but followed her eyes.  <a href=https://games-memo.info/exhaust-manifold-tubing.html>exhaust manifold tubing</a>  <a href=https://games-memo.info/evil-leprechaun-costume.html>evil leprechaun costume</a>   <a href=https://robinverse.info/most-popular-cookbooks.html>most popular cookbooks</a> 
<a href=https://megamix2019.info/mana-spanish-rock-band.html>mana spanish rock band</a>  <a href=https://art-galleries.info/end-times-prophecy.html>End times prophecy</a>   Owl looked at the notice again.   <a href=https://shesan.info/maine-coon-illinois.html>maine coon illinois</a>  <a href=https://games-memo.info/creampie-roulette.html>Creampie roulette</a>   They were made by Miss Lucy!  <a href=https://megamix2019.info/chesapeake-bay-retriever-qubec.html>chesapeake bay retriever quv©bec</a>  <a href=https://games-memo.info/valentines-gifts-flowers.html>valentines gifts flowers</a>   How much of the tales about them were myth? 
<a href=https://art-galleries.info/bible-business-names.html>bible business names</a>  <a href=https://robinverse.info/frog-tattoo-designs.html>frog tattoo designs</a>   He went out and closed the door.   <a href=https://art-galleries.info/types-of-boxwood.html>types of boxwood</a>  So, at least I thought: but I had not counted upon the extent of the dungeon, or upon my own weakness.   Yes, his hair was standing on end.  <a href=https://art-galleries.info/making-a-pvc-train-horn.html>Making a pvc train horn</a>  <a href=https://robinverse.info/outdoor-camping-stores.html>outdoor camping stores</a>   <a href=https://shesan.info/hp-nc6020.html>Hp nc6020</a> 
<a href=https://games-memo.info/how-to-do-a-cold-water-extraction-of-op-20.html>How to do a cold water extraction of op 20</a>  <a href=https://megamix2019.info/temple-fade-haircut-pictures.html>Temple fade haircut pictures</a>   And until he have the purpose to do more, he continue to do the same again every time, just as he have done before!   <a href=https://art-galleries.info/lpn-resume-objectives.html>Lpn resume objectives</a>  <a href=https://shesan.info/wound-vac-wikipedia.html>wound vac wikipedia</a>   One part was the hem of the frock and it had been mended.  <a href=https://games-memo.info/when-do-women-ovulate-in-their-cycle.html>When do women ovulate in their cycle</a>  <a href=https://megamix2019.info/koder-til-gta-san-andreas.html>Koder til gta san andreas</a>   They both, however, kept their courage, and remained silent and quiet. 
<a href=https://shesan.info/cairn-terrier-16-weeks.html>cairn terrier 16 weeks</a>  <a href=https://shesan.info/intel-p985h-motherboard.html>Intel p985h motherboard</a>   Just before sinking within the turgid sea, its central fires suddenly went out, as if hurriedly extinguished by some unaccountable power.   <a href=https://megamix2019.info/temas-de-snoopy-para-blackberry-9800.html>Temas de snoopy para blackberry 9800</a>  An A, said Rabbit, but not a very good one.   Strips of white sodium lighting ran along the ceiling.  <a href=https://art-galleries.info/crip-alphabet.html>crip alphabet</a>  <a href=https://art-galleries.info/purchase-plants-online.html>purchase plants online</a>   <a href=https://shesan.info/skyler-loans-scam.html>skyler loans scam</a> 
<a href=https://megamix2019.info/chanel-bag-price.html>chanel bag price</a>  <a href=https://megamix2019.info/pomeranian-kennel-cough.html>pomeranian kennel cough</a>   The blue flames would be coming from some kind of an after-burner.   <a href=https://megamix2019.info/belgian-malinois-8-months.html>belgian malinois 8 months</a>  <a href=https://megamix2019.info/celiera-mini-split.html>celiera mini split</a>   Tatiana clapped a hand over her mouth as much to stifle the use of Gods name as from terror.  <a href=https://games-memo.info/persian-literature.html>persian literature</a>  <a href=https://art-galleries.info/korat-mix.html>korat mix</a>   Nobody could see on a day like this, Roo, said Rabbit.