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.
![](https://i-blog.csdnimg.cn/blog_migrate/fa435624375c2ac7978932baea1eb76b.png)
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.
![](https://i-blog.csdnimg.cn/blog_migrate/03a33e9bdf1799b3a7ac3711f735b9a8.png)
![](https://i-blog.csdnimg.cn/blog_migrate/40458f5724760b5a5b34193d224eb113.png)
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.
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.
Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe.
2 1 2 3 4 5 6
5
Picture for the sample test. Green color denotes the additional street lights.
![](https://i-blog.csdnimg.cn/blog_migrate/bddca836cbf0268d7dd5957f7eb7e1b5.png)
codeforces太练智商了,md,智商弱老是被虐成狗。
题意:要是从最顶层到最底层的每一条路的灯个数相等,现在给出每条边的灯数,求最少需要增加多少灯。
思维:能在更上面一层增加的在上面增加,因为你在下面增加的话只有上面需要增加,在下面增加的话两条都要增加。
所以可以贪心的选择当下面部分不同的话增加灯使得两部分相等,这样选择就避免了每次修改都要修改到下面部分,最优子结构,重复子问题,无后效都满足。用记忆化搜索解
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
typedef long long ll;
int t,n,m;
int a[maxn];
int dp[maxn];
int d[maxn];
int ans;
int dfs(int root){
if(d[root]==0&&root!=1)return 0;
if(dp[root]>0)return dp[root];
int res=abs(dfs(root<<1)-dfs((root<<1)+1));
ans+=res;
return dp[root]=d[root]+res+min(dp[root<<1],dp[(root<<1)+1]);
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d",&n);
memset(d,0,sizeof d);
memset(dp,0,sizeof dp);
for(int i=2;i<=((1<<(n+1))-1);i++){
scanf("%d",&d[i]);
}
dfs(1);
printf("%d\n",ans);
}