B. Om Nom and Dark Park

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/526/problem/B

Description

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 Input

2
1 2 3 4 5 6

Sample Output

5

HINT

题意

让你从根走到每个叶子所要的花费都相同,请问最少得增加多少边权?

题解:

啊,DFS,自底往上,预处理个前缀和的东西

然后ans加上左右叶子的差值,然后一直跑DFS就是了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/* int buf[10];
inline void write(int i) {
int p = 0;if(i == 0) p++;
else while(i) {buf[p++] = i % 10;i /= 10;}
for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);
printf("\n");
}
*/
//************************************************************************************** inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
ll powmod(ll a,ll b) {
ll ans=;
for(ll i=;i<=b;i++)
ans*=a;
return ans;
}
ll a[maxn];
ll dp[maxn];
ll n;
void read_dfs(int x)
{
if(x>=powmod(,n))
return;
read_dfs(x*);
read_dfs(x*+);
a[x*]+=max(a[x**],a[x**+]);
a[x*+]+=max(a[(x*+)*],a[(x*+)*+]);
//cout<<a[x*2]<<" "<<x*2<<endl;
//cout<<a[x*2+1]<<" "<<x*2+1<<endl;
}
ll dfs(ll x)
{
if(dp[x])
return dp[x];
if(x>=powmod(,n))
return ;
ll sum=;
dp[x]=dfs(x*)+dfs(x*+)+abs(a[x*]-a[x*+]);
return dp[x];
}
int main()
{
cin>>n;
int k=powmod(,n);
for(int i=;i<=n;i++)
{
for(int j=;j<powmod(,i);j++)
{
cin>>a[powmod(,i)+j];
}
}
read_dfs();
cout<<dfs()<<endl;
}

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

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

    Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who l ...

  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. 磁盘性能分析之iotop

    一.安装. yum install iotop [root@localhost tmp]# iotop -o iotop命令的键盘快捷键: 1.左右箭头改变排序方式,默认是按IO排序 2.r键是反向排 ...

  2. mysql -> 启动&多实例_03

    常用的连接方式: 套接字: mysql -uroot -p123 -S /application/mysql/tmp/mysql.sock Tcp/Ip: mysql -uroot -p123 -h ...

  3. http://s22.app1105796624.qqopenapp.com/

    http://s22.app1105796624.qqopenapp.com/ http://121.43.114.69/xiyou/app/js/ac_tx.js http://hiyouba.co ...

  4. centos7 yum搭建lamp环境

    =============================================== 2018/1/14_第1次修改                       ccb_warlock == ...

  5. 数据库-mysql触发器

    MySQL包含对触发器的支持.触发器是一种与表操作有关的数据库对象,当触发器所在表上出现指定事件时,将调用该对象,即表的操作事件触发表上的触发器的执行. 一:创建触发器 在MySQL中,创建触发器语法 ...

  6. HTTP Headers解析

    什么是HTTP Headers? 它包含了哪些内容? 利用requests.get()函数对豆瓣读书进行请求, 返回的r.headers如下所示: >>> import reques ...

  7. Centos7.3安装和配置jre1.8

    在正式环境里 我们可以不安装jdk ,仅仅安装Java运行环境 jre即可: 第一步:下载jre 我们去oracle官方下载下jre http://www.oracle.com/technetwork ...

  8. sql newid()随机函数

    从A表随机取2条记录,用SELECT TOP 10 * FROM ywle order by newid()order by 一般是根据某一字段排序,newid()的返回值 是uniqueidenti ...

  9. SQLSERVER中的人民币数字转大写的函数实现

    CREATE  FUNCTION [dbo].[f_num_chn] (@num numeric(14,5))RETURNS varchar(100) WITH ENCRYPTIONASBEGIN-- ...

  10. c 语言文本文件判断是否到达结尾的问题

    在c语言中,判断文件结尾有两种方法,第一种是使用feof()函数,feof(fp)用于测试fp所指向的文件的当前状态是否为“文件结束”.如果是,函数则返回的是非0值(真),否则为0(假),要注意的是, ...