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 + 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 2n, 2n + 1, ..., 2n + 1 - 1 and
these exits lead straight to the Om Nom friends' houses. From each square i (2 ≤ i < 2n + 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 n roads.


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  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 2n + 1 - 2 numbers a2, a3, ... a2n + 1 - 1 —
the initial numbers of street lights on each road of the park. Here ai is
the number of street lights on the road between squares i and .
All numbers ai 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.

Sample test(s)
input
2
1 2 3 4 5 6
output
5
Note

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

这题可以把父节点和子节点之间的距离全部算作是子节点上的值,这样除了根节点外,子节点恰好每个有一个值。因为每一个父节点的两个子节点最后的值不可能同时增加(因为如果有同时增加的情况,可以直接加在父节点上),所以每次都是一个子节点的增加相应的值和另一个子节点相同,所以可以从倒数第二层开始向前递归。

#include<stdio.h>
#include<math.h>
#include<string.h>
int max(int a,int b)
{
return a>b?a:b;
}
int a[3000];
int main()
{
int n,m,i,j,b,ans;
while(scanf("%d",&n)!=EOF)
{
for(i=2;i<=pow(2,n+1)-1;i++)
{
scanf("%d",&b);
a[i]=b;
}
ans=0;
for(i=pow(2,n)-1;i>=1;i--)
{
a[i]+=max(a[i*2],a[i*2+1]);
ans=ans+2*max(a[i*2],a[i*2+1])-a[i*2]-a[i*2+1];
//printf("%d\n",ans);
}
printf("%d\n",ans);
}
return 0;
}

ZeptoLab Code Rush 2015 B. Om Nom and Dark Park的更多相关文章

  1. ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS

    B. Om Nom and Dark Park Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  2. Codeforces - ZeptoLab Code Rush 2015 - D. Om Nom and Necklace:字符串

    D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力

    C. Om Nom and Candies Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526 ...

  4. ZeptoLab Code Rush 2015 C. Om Nom and Candies [ 数学 ]

    传送门 C. Om Nom and Candies time limit per test 1 second memory limit per test 256 megabytes input sta ...

  5. Codeforces ZeptoLab Code Rush 2015 D.Om Nom and Necklace(kmp)

    题目描述: 有一天,欧姆诺姆发现了一串长度为n的宝石串,上面有五颜六色的宝石.他决定摘取前面若干个宝石来做成一个漂亮的项链. 他对漂亮的项链是这样定义的,现在有一条项链S,当S=A+B+A+B+A+. ...

  6. CodeForces ZeptoLab Code Rush 2015

    拖了好久的题解,想想还是补一下吧. A. King of Thieves 直接枚举起点和5个点之间的间距,进行判断即可. #include <bits/stdc++.h> using na ...

  7. B. Om Nom and Dark Park

    B. Om Nom and Dark Park 在满二叉树上的某些边上添加一些值.使得根节点到叶子节点的路径上的权值和都相等.求最少需要添加多少. 我们利用性质解题.   考察兄弟节点.由于他们从跟节 ...

  8. Zepto Code Rush 2014 B - Om Nom and Spiders

    注意题目给的是一个nxm的park,设元素为aij,元素aij 有4种可能U(上移),D(下移),L(左移),R(右移) 假设第i行第j列元素aij(注意元素的索引是从0开始的) 当aij为D时,此时 ...

  9. CF Zepto Code Rush 2014 B. Om Nom and Spiders

    Om Nom and Spiders time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. Python模块化编程与装饰器

    Python的模块化编程 我们首先以一个例子来介绍模块化编程的应用场景,有这样一个名为requirements.py的python3文件,其中两个函数的作用是分别以不同的顺序来打印一个字符串: # r ...

  2. docker logs 查看容器日志操作

    查看日志 官方文档:https://docs.docker.com/engine/reference/commandline/logs/ # 查看指定数量的实时日志 # docker logs -tf ...

  3. 【Zabbix】配置zabbix agent向多个server发送数据

    1.背景: server端: 172.16.59.197  ,172.16.59.98 agent 端: hostname:dba-test-hzj02 IP:172.16.59.98 2.方式: 配 ...

  4. SQL Server management studio使用sa连接时报错与伺服器的连接已成功,但在登入程序是发生错误

    使用Sql Server management studio的sa用户连接数据库时,报如下错误 解决方法: 1.使用windows验证登录 2.右键点击连接,点击属性,点击安全性,选择混合验证 3.重 ...

  5. Windows10下Canvas对象获得屏幕坐标不正确的原因排查与处理

    因为Canvas没有直接将画布内容保存为图片的方法,所以很多时候是通过获得Canvas画布的坐标,然后通过截图的方式来将画布内容保存为本地图片. 如何取得Canvas画布的坐标呢,比较简单实用的方式如 ...

  6. 边缘计算k8s集群SuperEdge初体验

    前言 手上一直都有一堆的学生主机,各种各样渠道途径拿来的机器. 一直管理里面都比较蛋疼,甚至也不太记得住它们在哪是什么IP,管理起来很是头疼. 有阵子空闲的时候想折腾了一下边缘计算集群方案. 希望能把 ...

  7. 24V降压5V芯片,5A,4.5V-30V输入,同步降压调节器

    PW2205开发了一种高效率的同步降压DC-DC转换器5A输出电流.PW2205在4.5V到30V的宽输入电压范围内工作集成主开关和同步开关,具有非常低的RDS(ON)以最小化传导损失.PW2205采 ...

  8. Java流程控制与Scanner类的使用

    Java流程控制与Scanner类的使用 Scanner类 Scanner类可以使程序接受键盘输入,实现人机交互 一个完整的Scanner的使用例子: //创建一个扫描器对象,用于接收键盘数据 Sca ...

  9. python(pymysql操作数据库)

    第一种方式 import pymysql # 打开数据库连接 db = pymysql.connect(host="192.168.88.11", user="root& ...

  10. 等待 Redis 应答 Redis pipeline It's not just a matter of RTT

    小结: 1.When pipelining is used, many commands are usually read with a single read() system call, and ...