博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZeptoLab Code Rush 2015 B. Om Nom and Dark Park
阅读量:5025 次
发布时间:2019-06-12

本文共 2814 字,大约阅读时间需要 9 分钟。

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 test(s)
input
21 2 3 4 5 6
output
5
Note

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

这题可以把父节点和子节点之间的距离全部算作是子节点上的值,这样除了根节点外,子节点恰好每个有一个值。因为每一个父节点的两个子节点最后的值不可能同时增加(因为如果有同时增加的情况,可以直接加在父节点上),所以每次都是一个子节点的增加相应的值和另一个子节点相同,所以可以从倒数第二层开始向前递归。

#include
#include
#include
int max(int a,int b){
return a>b?a:b;}int a[3000];int main(){
int n,m,i,j,b,ans; while(scanf("%d",&n)!=EOF) {
for(i=2;i<=pow(2,n+1)-1;i++) {
scanf("%d",&b); a[i]=b; } ans=0; for(i=pow(2,n)-1;i>=1;i--) {
a[i]+=max(a[i*2],a[i*2+1]); ans=ans+2*max(a[i*2],a[i*2+1])-a[i*2]-a[i*2+1]; //printf("%d\n",ans); } printf("%d\n",ans); } return 0;}

转载于:https://www.cnblogs.com/herumw/p/9464852.html

你可能感兴趣的文章
面向对象编程感悟
查看>>
[NOI2015]荷马史诗
查看>>
找规律+模拟 之 codevs 1160 蛇形矩阵
查看>>
第三次java作业
查看>>
Mysql数据库操作语句总结(二)
查看>>
git开发问题! [remote rejected] master -> master (pre-receive hook declined)
查看>>
安装Oracle BPM11g R1
查看>>
OpenLDAP权限配置
查看>>
Java中Array.sort()的几种用法
查看>>
数据库启动和关闭的几种方式
查看>>
$.ajax 使用详解
查看>>
移动web资源整理
查看>>
剪切板-监视剪贴板
查看>>
获取sd卡的总大小和可用大小
查看>>
[转]国外Oracle专家服务报价
查看>>
python 面试题
查看>>
an exceptiion has occurred while trying to add a view to region错误
查看>>
原生js面向对象实现移动端轮播图
查看>>
Parallel Python 并行开发多核心并行程序
查看>>
解决80端口占用
查看>>