CF526B.Om Nom and Dark Park

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

The park consists of 2n+112^{n+1}-1 squares connected by roads so that the scheme of the park is a full binary tree of depth nn . More formally, the entrance to the park is located at the square 11 . The exits out of the park are located at squares 2n,2n+1,...,2n+112^{n},2^{n}+1,...,2^{n+1}-1 and these exits lead straight to the Om Nom friends' houses. From each square ii ( 2<=i<2n+12<=i<2^{n+1} ) there is a road to the square . Thus, it is possible to go from the park entrance to each of the exits by walking along exactly nn roads.

To light the path roads in the evening, the park keeper installed street lights along each road. The road that leads from square ii to square has aia_{i} 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.

输入格式

The first line contains integer nn ( 1<=n<=101<=n<=10 ) — the number of roads on the path from the entrance to any exit.

The next line contains 2n+122^{n+1}-2 numbers a2,a3,... a2n+11a_{2},a_{3},...\ a_{2^{n+1}-1} — the initial numbers of street lights on each road of the park. Here aia_{i} is the number of street lights on the road between squares ii and . All numbers aia_{i} are positive integers, not exceeding 100100 .

输出格式

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

输入输出样例

  • 输入#1

    2
    1 2 3 4 5 6
    

    输出#1

    5
    

说明/提示

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

首页