vj Om Nom and Dark Park

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 aiis 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

Input
2
1 2 3 4 5 6
Output
5

Hint

Picture for the sample test. Green color denotes the additional street lights.

题目大意:如图,有这么一个二叉树形的道路,1为入口,叶子为出口,从2开始到最后,有一个值表示从i到i/2取整的值的路灯数目,问还要添加至少多少等使得每条道路(从1到叶子算一条道路)上的灯的数目相等。

题目大意:此题一看就要往二叉树上靠,怎样才能使得每条路径一样呢?那就要先找出路径中的最大的,让其在根节点,然后类似的对每个节点找出该节点到自己孩子的叶子的最大的路径上的灯的数目,然后在从根节点开始往下遍历,每个节点的sum-该节点的data在减去该节点的孩子的sum值就是这两点的路上要添加的路灯数目。在找最大sum时,类似与RMQ.递归的终止条件用深度判断。这样就能找出添加最少的灯数了(毕竟先添加主干路上的灯时才是添加的最少的)

#include <iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
struct tree
{
    int data;
    int sum;
}a[1<<11];
 int n;
 int ans;
 void RMQ(int node,int d)
 {
     if(d==n)
     {
         a[node].sum+=a[node].data;
         return ;
     }
     else
     {
         RMQ(2*node,d+1);
         RMQ(2*node+1,d+1);
         a[node].sum=max(a[node*2].sum,a[node*2+1].sum)+a[node].data;
     }
 }

 void dfs(int node,int d)
 {
     if(d==n)
     {
         return ;
     }
     else
     {
         dfs(node*2,d+1);
         dfs(2*node+1,d+1);
         ans+=a[node].sum-a[node].data-a[2*node].sum;
         ans+=a[node].sum-a[node].data-a[2*node+1].sum;
     }
 }
int main()
{
    while(~scanf("%d",&n))
    {
        a[1].data=0;
        a[1].sum=0;
        for(int i=2;i< 1<<(n+1);i++)
        {
            scanf("%d",&a[i].data);
            a[i].sum=0;
        }
        RMQ(1,0);
        ans=0;
        dfs(1,0);
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值