题干:

Two pirates Polycarpus and Vasily play a very interesting game. They have n chests with coins, the chests are numbered with integers from 1 to n. Chest number i has aicoins.

Polycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer x (2·x + 1 ≤ n) and take a coin from each chest with numbers x, 2·x, 2·x + 1. It may turn out that some chest has no coins, in this case the player doesn't take a coin from this chest. The game finishes when all chests get emptied.

Polycarpus isn't a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his moves, he also counts Vasily's moves.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of chests with coins. The second line contains a sequence of space-separated integers: a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai is the number of coins in the chest number i at the beginning of the game.

Output

Print a single integer — the minimum number of moves needed to finish the game. If no sequence of turns leads to finishing the game, print -1.

Examples

Input

1
1

Output

-1

Input

3
1 2 3

Output

3

Note

In the first test case there isn't a single move that can be made. That's why the players won't be able to empty the chests.

In the second sample there is only one possible move x = 1. This move should be repeated at least 3 times to empty the third chest.

题目大意:

有n个盒子,告诉你每个盒子中小球的个数(盒子不空!!这很重要)。现在给你 一次操作 的定义:选择编号为x的盒子,从位置为x,2*x,2*x+1这三个盒子中各拿走一个球(如果盒子已空则不拿。)问你最少几次操作可以把所有盒子都拿空。如果无法达到目的,,则输出-1。

解题报告:

首先要知道何时是怎么也拿不走的,,,观察发现如果盒子是偶数个的话。。怎么都达不到目的。。或者盒子数小于3,也是这样。(因为我一次就要拿三个嘛)(读题啊读题啊!!英语啊英语啊!!)

解法就是倒着拿,,因为最右边的必须要拿走,并且拿走的方法很唯一那就是当成2*x+1那一项,所以我们肯定先拿右边的啊,顺道着就拿走了左边的了。。于是贪心emmm

其实这题不需要用while那样,,可以直接减到零,,但是这题数据量小,所以怎么搞都可以。。考察点在算法的设计而不在优化。。。

AC代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll a[MAX];
ll n,ans;
int main()
{cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i);if(n%2 == 0 || n<3) {puts("-1");return 0 ;}for(int i = n; i>=1; i--) {while(a[i]>0) {if(i&1) {a[i]--;a[i-1]--;a[(i-1)>>1]--;}else {a[i]--;a[i+1]--;a[i>>1]--;}ans++;} }printf("%lld\n",ans);return 0 ;}

【CodeForces - 245C 】Game with Coins (思维,贪心)相关推荐

  1. CodeForces - 618D Hamiltonian Spanning Tree(思维+贪心)

    题目链接:点击查看 题目大意:首先给出n个点,n*(n-1)/2条边组成的无向图,边的权值为y,现在给出一棵连接n个点的树,树上的权值都是x,现在问如何在每个点只遍历一次的情况下走遍n个点,并使一路上 ...

  2. codeforces#320(div2) D Or Game 贪心

    codeforces#320(div2) D  "Or" Game  贪心 D. "Or" Game time limit per test 2 seconds ...

  3. Codeforces 1077B Disturbed People(思维题)

    Codeforces 1077B Disturbed People(思维题) There is a house with nn flats situated on the main street of ...

  4. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  5. F 魏迟燕的自走棋(思维+贪心+并查集维护联通块/左部点配对边<=2的匈牙利)

    https://ac.nowcoder.com/acm/contest/9984/F 参考:F 魏迟燕的自走棋(贪心+并查集) 将每个人看成一个点,武器的能力值抽象成边,这样就转化成图论的模型了. 然 ...

  6. CodeForces - 1395D - Boboniu Chats with Du 贪心

    CodeForces - 1395D - Boboniu Chats with Du 贪心 题意:如果ai>ma_i>mai​>m,并且当天可以说话,则接下来ddd天不能说话.其余所 ...

  7. Codeforces Round #459 (Div. 2) C 思维,贪心 D 记忆化dp

    Codeforces Round #459 (Div. 2) C. The Monster 题意:定义正确的括号串,是能够全部匹配的左右括号串. 给出一个字符串,有 (.). ? 三种字符, ? 可以 ...

  8. codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)

    题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...

  9. CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)

    ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...

最新文章

  1. Win64 驱动内核编程-28.枚举消息钩子
  2. 查看服务器cpu是否支持VT
  3. TCP发送接口(如send(),write()等)的返回值与成功发送到接收端的数据量无直接关系
  4. 算命数据_未来的数据科学家或算命精神向导
  5. 基于xml技术的操作
  6. mysql操作符_MySql 中的=操作符
  7. -Android的发展webservice-号码归属地查询
  8. 屏蔽五项网络功能 让XP系统极速狂飙
  9. 图的遍历之DSF深度优先算法6.2.1(网络整理)
  10. 狂神说笔记之ElasticSearch
  11. js worker使用总结
  12. [ZT]Inside WINDOWS NT Object Manager
  13. RabbitMQ 可靠性、重复消费、顺序性、消息积压解决方案
  14. 【防坑指南】nginx重启后出现[error] open() “/usr/local/var/run/nginx/nginx.pid” failed
  15. QCon演讲实录|基于 KAITIAN 的前端工程研发模式变革
  16. 【机器学习】线性判别式(LDA/FLD)
  17. 对于String作为 HashMap key 的一些思考。
  18. 黑莓9900(9930)网页在线播放音乐
  19. 后端程序员之路 11、初高中数学复习
  20. 新会计准则 计算机管理系统,全总新工会会计制度管理系统软件

热门文章

  1. 花花酱leetcode 题目-二分搜索
  2. 【Breadth-first Search 】785. Is Graph Bipartite?
  3. HDU - 4586 数学期望
  4. 最长非降子序列(动态规划dp dynamic programming)
  5. 12v小型电机型号大全_鄂破碎机型号大全图,小型鄂破碎机价格
  6. 重燃你的PHP安全之火.pdf,读《重燃你的php之火》总结笔记
  7. Oracle的Net Configuration Assistant 配置
  8. win7蓝屏0x000000f4修复_注意:关于近期多数电脑蓝屏的处理和预防方法
  9. 【转】细说.NET中的多线程 (三 使用Task)
  10. Sharepoin学习笔记—架构系列--08 Sharepoint的数据模型(DataModel)、数据管理(Data Management)与查询(Query System)