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. 来不及解释!Linux常用命令大全,先收藏再说

    摘要:Linux常用命令,很适合你的. 一提到操作系统,我们首先想到的就是windows和Linux.Windows以直观的可视化的方式操作,特别适合在桌面端PC上操作执行相应的软件.相比较Windo ...

  2. 2021年正确的Android逆向开发学习之路

    2021年正确的Android逆向开发学习之路 说明 文章首发于HURUWO的博客小站,本平台做同步备份发布.如有浏览或访问异常或者相关疑问可前往原博客下评论浏览. 原文链接 2021年正确的Andr ...

  3. kubernets之存活探针

    一   存活探针存在的意义 1.1  kubernet通过存活探针(liveness probe)检查容器是否还在运行,可以为pod中的每个容器单独指定存活探针,如果探针执行失败,kubernets会 ...

  4. Sklearn 与 TensorFlow 机器学习实战—一个完整的机器学习项目

    本章中,你会假装作为被一家地产公司刚刚雇佣的数据科学家,完整地学习一个案例项目.下面是主要步骤: 项目概述. 获取数据. 发现并可视化数据,发现规律. 为机器学习算法准备数据. 选择模型,进行训练. ...

  5. 如何在C#中使用MSMQ

    MSMQ (Microsoft消息队列)是Windows中默认可用的消息队列.作为跨计算机系统发送和接收消息的可靠方法,MSMQ提供了一个可伸缩.线程安全.简单和使用方便的队列,同时为你提供了在Win ...

  6. 【1w字+干货】第一篇,基础:让你的 Redis 不再只是安装吃灰到卸载(Linux环境)

    Redis 基础以及进阶的两篇已经全部更新好了,为了字数限制以及阅读方便,分成两篇发布. 本篇主要内容为:NoSQL 引入 Redis ,以及在 Linux7 环境下的安装,配置,以及总结了非常详细的 ...

  7. C#高级编程第11版 - 第六章 索引

    [1]6.2 运算符 1.&符在C#里是逻辑与运算.管道符号|在C#里则是逻辑或运算.%运算符用来返回除法运算的余数,因此当x=7时,x%5的值将是2. [2]6.2.1 运算符的简写 1.下 ...

  8. java虚拟机——轻松搞懂jvm

    一.JVM体系结构概述 JVM位置 JVM体系结构 1.1 类加载器 ClassLoader   类加载器(ClassLoader)负责加载class文件,class文件在文件开头有特定的文件标示,并 ...

  9. http发送

    package cn.com.yitong.wdph.util; import java.io.BufferedReader;import java.io.InputStream;import jav ...

  10. 一个基于protocol buffer的RPC实现

    Protocol Buffer仅仅是提供了一套序列化和反序列化结构数据的机制,本身不具有RPC功能,但是可以基于其实现一套RPC框架. Services protocol buffer的Service ...