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

时间:2023-11-28 13:33:02

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.

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

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 ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS. Thus, it is possible to go from the park entrance to each of the exits by walking along exactly n roads.

ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFSTo 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 ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS 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 ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS. 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;
}