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. Mac安装homebrew,postman,charles

    Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能.简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷. 1. ...

  2. 【RAC】打完补丁后,发现只有一台rac可以启动,另一台无法启动

    安装11Gr2单机asm后,打完11.2.0.3.7的psu后,发现启动不起来数据库,alert日志内容如下: Errors in file /u01/app/oracle/diag/rdbms/bd ...

  3. Angular学习资料大全和常用语法汇总(让后端程序员轻松上手)

    前言: 首先为什么要写这样的一篇文章呢?主要是因为前段时间写过一些关于Angualr的相关实战文章,有些爱学习的小伙伴对这方面比较感兴趣,但是又不知道该怎么入手(因为认识我的大多数小伙伴都是后端的同学 ...

  4. Android事件分发机制三:事件分发工作流程

    前言 很高兴遇见你~ 本文是事件分发系列的第三篇. 在前两篇文章中,Android事件分发机制一:事件是如何到达activity的? 分析了事件分发的真正起点:viewRootImpl,Activit ...

  5. Spring Boot(IDEA,Gradle)超详细用户管理项目(一)——Hello World

    1.构建工具的配置(Gradle):自定义-所有设置:构建.执行.部署-构建工具-Gradle: 设置Gradle用户主目录:(该目录相当于仓库,gradle将下载所需依赖到此目录下),此目录下可新建 ...

  6. watchdog应用实例

    watchdog应用实例 By 鬼猫猫 20130504 http://www.cnblogs.com/muyr/ 实例:监测某文件夹,一旦文件夹里有文件,就把它剪切到其他服务器 import sys ...

  7. java之 Request

    0x01.Request 什么是request 在Servlet API中,定义了一个HttpServletRequest接口,它继承自ServletRequest接口,专门用来封装HTTP请求消息. ...

  8. 切片声明 切片在内存中的组织方式 reslice

    数组是具有相同 唯一类型 的一组已编号且长度固定的数据项序列(这是一种同构的数据结构),[5]int和[10]int是属于不同类型的.数组的编译时值初始化是按照数组顺序完成的(如下). 切片声明方式, ...

  9. TCMalloc源码学习(三)(小块内存分配)

    线程本地cache 线程本地cache对应的是类 ThreadCache,每一个thread一个实例,初始化代码在static函数CreateCacheIfNecessary中, 在该线程第一次申请内 ...

  10. setTimeout、Promise、Async/Await 的区别

    事件循环中分为宏任务队列和微任务队列其中setTimeout的回调函数放到宏任务队列里,等到执行栈清空以后执行promise.then里的回调函数会放到相应宏任务的微任务队列里,等宏任务里面的同步代码 ...