Problem:
Score : 600 points

Problem Statement

Given is an integer sequence of length N+1: A_0, A_1, A_2, \ldots, A_N. Is there a binary tree of depth N such that, for each d = 0, 1, \ldots, N, there are exactly A_d leaves at depth d? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print -1.

Notes

A binary tree is a rooted tree such that each vertex has at most two direct children.

A leaf in a binary tree is a vertex with zero children.

The depth of a vertex v in a binary tree is the distance from the tree's root to v. (The root has the depth of 0.)

The depth of a binary tree is the maximum depth of a vertex in the tree.

Constraints

0 \leq N \leq 10^5

0 \leq A_i \leq 10^{8} (0 \leq i \leq N)

A_N \geq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 A_2 \cdots A_N

Output

Print the answer as an integer.

Sample Input 1

3
0 1 1 2

Sample Output 1

7

Below is the tree with the maximum possible number of vertices. It has seven vertices, so we should print 7.

Sample Input 2

4
0 0 1 0 2

Sample Output 2

10

Sample Input 3

2
0 3 1

Sample Output 3

-1

Sample Input 4

1
1 1

Sample Output 4

-1

Sample Input 5

10
0 0 1 1 2 3 5 8 13 21 34

Sample Output 5

264

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) { int g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; }

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a)  begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

class RangeNode {
public:
  RangeNode() {
    init();
  }

  void init() {
    delay = delayFlag = 0;
    val = 0;
  }

  //クエリ処理
  static int evaluateQuery(int p1, int p2) 
  {
    return min(p1, p2);
  }

  //range処理
  void evaluateRange(int range) 
  {
    val += delay;
  }

  //delay処理
  void evaluateDelay(int d)
  {
    delay += d;
  }

  //単位元(クエリ処理)
  static int unit()
  {
    return ((long long)1<<60) - 1;
  }

  int val;
  int delay;
  bool delayFlag;

private:

};

template<class T, int Size>
class RangeSegmentTree {
public:
  RangeSegmentTree() 
  {
    init();
  }

  void init()
  {
    n = 1;
    while(n < Size) n *= 2;
    node.clear(); node.resize(2*n);
  }

  void evaluateRangeNode(int k, int l, int r)
  {
    if(node[k].delayFlag) {
      node[k].evaluateRange(r-l);
      if(r-l>1) {
        node[2*k+1].evaluateDelay(node[k].delay);
        node[2*k+2].evaluateDelay(node[k].delay);
        node[2*k+1].delayFlag = node[2*k+2].delayFlag = true;
      }
      node[k].delayFlag = false;
      node[k].delay = 0;
    }
  }

  void updateRange(int l, int r, int v)
  {
    _updateRange(l,r,v,0,0,n);
  }

  int query(int l, int r)
  {
    return _query(l,r,0,0,n);
  }

private:
  vector<T> node;
  int n;

  void _updateRange(int l, int r, int v, int k, int a, int b)
  {
    evaluateRangeNode(k,a,b);

    if(b <= l || r <= a) return;
    else if(l <= a && b <= r) {
      node[k].evaluateDelay(v);
      node[k].delayFlag = true;
      evaluateRangeNode(k,a,b);
      return;
    } else {
      int mid = (a+b)/2;
      _updateRange(l,r,v,2*k+1,a,mid);
      _updateRange(l,r,v,2*k+2,mid,b);
      node[k].val = RangeNode::evaluateQuery(node[2*k+1].val, node[2*k+2].val);
      return;
    }
  }

  int _query(int l, int r, int k, int a, int b)
  {
    evaluateRangeNode(k,a,b);
    
    if(b <= l || r <= a) return RangeNode::unit();
    else if(l <= a && b <= r) {
      return node[k].val;
    } else {
      int mid = (a+b)/2;
      int vl = _query(l,r,2*k+1,a,mid);
      int vr = _query(l,r,2*k+2,mid,b);
      return RangeNode::evaluateQuery(vl,vr);
    }
  }
};

const int MAX_N = 100005;

int N;
int A[MAX_N];
RangeSegmentTree<RangeNode, MAX_N> rst;

signed main()
{
  cin >> N;
  REP(n,N+1) {
    cin >> A[n];
  }

  if (N == 0) {
    if (A[0] == 1) {
      cout << 1 << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }

  if (0 < A[0]) {
    cout << -1 << endl;
    return 0;
  }

  int ans = 0;
  int d = 0;
  rst.updateRange(0,1,2);

  FOR(n,1,N+1) {
    int a = A[n];

    while(a) {
      if (d == n) {
        cout << -1 << endl;
        return 0;
      }

      int val = rst.query(d,n);

      if (val < a) {
        ans += (n-d)*val;
        rst.updateRange(d,n,-val);
        d++;
        a -= val;
      } else {
        ans += (n-d)*a;
        rst.updateRange(d,n,-a);
        a = 0;
        break;
      }
    }

    rst.updateRange(n,n+1, 2*rst.query(n-1,n));
  }

  cout << ans+1 << endl;

  return 0;
}