ZeptoLab Code Rush 2015
B
Om Nom and Dark Park
Codeforces
1400
原题
← AC →
翻译中…

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.

翻译中…

image

翻译中…

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 image. Thus, it is possible to go from the park entrance to each of the exits by walking along exactly $*n*$ roads.

翻译中…

image

翻译中…

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

翻译中…

image

翻译中…

has

翻译中…

ai

翻译中…

lights.

翻译中…

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.

翻译中…

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.

翻译中…

Input

翻译中…

The first line contains integer $*n*$ ($1 ≤ *n* ≤ 10$) — the number of roads on the path from the entrance to any exit.

翻译中…

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 image. All numbers $*a*_{*i*}$ are positive integers, not exceeding $100$.

翻译中…

Output

翻译中…

Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe.

翻译中…

Note

翻译中…

Picture for the sample test. Green color denotes the additional street lights.

翻译中…

image

翻译中…

样例

Input #1
2
1 2 3 4 5 6
Output #1
5
InputCopy OutputCopy
题目信息
题号
526B
时间限制
1 s
内存限制
256 MB
标签
DFS 及类似
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。