codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

526B.md (3002B)


      1 Source: https://codeforces.com/problemset/problem/526/B
      2 
      3 B. Om Nom and Dark Park
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him.
     22 
     23  ![](https://espresso.codeforces.com/26fa9ef1e99302468e420da92b75b4bb12f64a2c.png) 
     24 
     25 The park consists of 2*n* + 1 - 1 squares connected by roads so that the scheme of the park is a full binary tree of depth *n*. More formally, the entrance to the park is located at the square 1. The exits out of the park are located at squares 2*n*, 2*n* + 1, ..., 2*n* + 1 - 1 and these exits lead straight to the Om Nom friends' houses. From each square *i* (2 ≤ *i* < 2*n* + 1) there is a road to the square ![](https://espresso.codeforces.com/1040a5e20739aa4ff1bdd6fb60e6665f025a3f1e.png). Thus, it is possible to go from the park entrance to each of the exits by walking along exactly *n* roads.
     26 
     27  ![](https://espresso.codeforces.com/937f6c9f63488cb7a5785abd858898042c473421.png)  To light the path roads in the evening, the park keeper installed street lights along each road. The road that leads from square *i* to square ![](https://espresso.codeforces.com/1d68be37100780bc5f918255b6da4287c591da33.png) has *a**i* lights.
     28 
     29 Om Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe.
     30 
     31 He asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads.
     32 
     33 Input
     34 
     35 The first line contains integer *n* (1 ≤ *n* ≤ 10) — the number of roads on the path from the entrance to any exit.
     36 
     37 The next line contains 2*n* + 1 - 2 numbers *a*2, *a*3, ... *a*2*n* + 1 - 1 — the initial numbers of street lights on each road of the park. Here *a**i* is the number of street lights on the road between squares *i* and ![](https://espresso.codeforces.com/1d68be37100780bc5f918255b6da4287c591da33.png). All numbers *a**i* are positive integers, not exceeding 100.
     38 
     39 Output
     40 
     41 Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe.
     42 
     43 Examples
     44 
     45 Input
     46 
     47 ```
     48 2  
     49 1 2 3 4 5 6
     50 ```
     51 
     52 Output
     53 
     54 ```
     55 5
     56 ```
     57 
     58 Note
     59 
     60 Picture for the sample test. Green color denotes the additional street lights.
     61 
     62  ![](https://espresso.codeforces.com/8bf2560e1f9c312298f87b0c551e74d976fd4ff5.png)