ZeptoLab Code Rush B. Om Nom and Dark Park DFS

Source: Internet
Author: User

B. Om Nom and Dark Park

Time limit:1 Sec Memory limit:256 MB

Topic Connection

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 your to help him.

The park consists of 2n + 1-1 squares connected by roads so the scheme of the park was a full binar Y Tree of depth n. More formally, the entrance to the park was located at the Square 1.  The exits out of the park is located at Squares 2n, 2n + 1, ..., 2n + 1- 1 and these exits leads straight to the Om Nom friends ' houses. From each square i (2≤ i < 2n + 1) There are a road to the square. Thus, it's possible to go from the park entrance to each of the exits by walking along exactly n roads. /c19>

The path roads in the evening, the park keeper installed street lights along each road. The road that leads from square i to square have a i lights.

Om Nom loves counting lights on the the-the-to-his friend. Om Nom is afraid of spiders who live in the park, so he doesn ' t like to walk along roads that's not enough lit. What he wants are that the the-the-the-the-friends should has in total the same number of lights. That'll make him feel safe.

He asked you him install additional lights. Determine what minimum number of lights it was needed to additionally place on the park roads so that a path from the Entra nCE to any exit of the park contains the same number of street lights. You could 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 in the path from the Entran Ce to any exit.

The next line contains 2n + 1-2 numbers a2, a3, ... a 2n + 1-1-the initial numbers of street lights on each road of the park. Here a I was the number of the Street lights on the road between squares i and. All numbers ai is positive integers, not exceeding .

OutputPrint 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 OUTPUT5
HINT

Test instructions

Let you go from the root to each leaf the cost is the same, how much at least to increase the right edge?

Exercises

Ah, DFS, bottom up, preprocessing a prefix and something then ans plus the difference between left and right leaves, and then run DFS just the same

Code:

//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>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 200001#defineMoD 10007#defineEPS 1e-9//const int INF=0X7FFFFFFF; //infinitely LargeConst intinf=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 () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} ll Powmod (ll A,ll b) {ll ans=1;  for(LL i=1; i<=b;i++) ans*=A; returnans;} ll A[maxn];ll dp[maxn];ll N;voidRead_dfs (intx) {    if(X>=powmod (2, N)) return; Read_dfs (x*2); Read_dfs (x*2+1); A[x*2]+=max (a[x*2*2],a[x*2*2+1]); A[x*2+1]+=max (a[(x*2+1)*2],a[(x*2+1)*2+1]); //cout<<a[x*2]<< "" <<x*2<<endl; //cout<<a[x*2+1]<< "" <<x*2+1<<endl;}ll DFS (ll x) {if(Dp[x])returnDp[x]; if(X>=powmod (2, N)) return 0; ll Sum=0; DP[X]=dfs (x*2) +dfs (x*2+1) +abs (a[x*2]-a[x*2+1]); returndp[x];}intMain () {CIN>>N; intK=powmod (2, N);  for(intI=1; i<=n;i++)    {         for(intj=0; J<powmod (2, i); J + +) {cin>>a[powmod (2, i) +J]; }} Read_dfs (1); cout<<dfs (1) <<Endl;}

ZeptoLab Code Rush B. Om Nom and Dark Park DFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.