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: Wrong Answer)

/**
    *Makacha
**/
#include <bits/stdc++.h>
using namespace std;

template <typename T> inline void read (T &x) {bool b = 0; char c; while (!isdigit (c = getchar()) && c != '-');
if (c == '-') c = getchar(), b = 1; x = c - 48; while (isdigit(c = getchar())) x = (x<<3) + (x<<1) + c - 48; if (b)x=-x;}
template <typename T> inline void wrip(T x) {if (x > 9) wrip(x / 10); putchar(x%10 + 48); }

template <typename T> inline void write(T x) {if (x < 0) putchar('-'), x = -x; wrip(x); putchar(' ');}
template <typename T> inline void writeln(T x) {if (x < 0) putchar('-'), x = -x; wrip(x); putchar('\n');}
inline char readch() {char c; do c = getchar(); while (c == '\n' || c == ' '); return c;}
inline void reads(string &s) {char c; while((c=getchar())==' '||c =='\n'); s=c; while((c=getchar())!=' '&&c!='\n'&&c!=EOF) s+=c;}
inline void getlines(string &s) {char c; while((c=getchar())=='\n'); s=c; while((c=getchar())!='\n') s+=c;}
typedef const int csint;

#define fi first
#define se second
#define mp make_pair
typedef unsigned long long ll;
typedef long double ld;
typedef pair <int, ll> ii;
typedef vector <ll> vi;

const ld pi = acos(-1);
const ll base = 1e9;
const int inf = 1e9,
N = 2e5 + 11;

int n;
vi a[N], node[N], res;

void fix(vi &a) {
    a.push_back(0);
    for (int i = 0; i < a.size() - 1; i++) {
        if (a[i] >= base)
            a[i + 1]++,
            a[i] -= base;
        if (a[i] < 0)
            a[i + 1]--,
            a[i] += base;
    }
    while (a.back() == 0 && a.size() > 1)
        a.pop_back();
}
void operator += (vi &a, vi b) {
    a.resize(max(a.size(), b.size()));
    for (int i = 0; i < b.size(); i++)
        a[i] += b[i];
    fix(a);
}
vi operator + (vi a, vi b) {
    a += b;
    return a;
}
vi operator - (vi a, vi b) {
    for (int i = 0; i < b.size(); i++)
        a[i] -= b[i];
    return a;
}
vi operator * (vi a, int b) {
    for (int i = 0; i < a.size(); i++)
        a[i] *= b;
    fix(a);
    return a;
}
bool operator < (vi a, vi b) {
    if (a.size() < b.size())
        return true;
    if (a.size() > b.size())
        return false;
    for (int i = a.size() - 1; i >= 0; i--)
        if (a[i] != b[i])
            return a[i] < b[i];
    return false;
}
void write(vi a) {
    cout << a.back();
    for (int i = a.size() - 2; i >= 0; i--)
        cout << setw(9) << setfill('0') << a[i];
}
int main() {
    #ifdef makacha
        freopen("m.inp", "r", stdin);
        freopen("m.out", "w", stdout);
    #endif //makacha
    read(n);
    for (int i = 0, x; i <= n; i++) {
        read(x);
        a[i].push_back(x);
    }
    node[0].push_back(1 - a[0][0]);
    for (int i = 1; i < n; i++) {
        if (node[i - 1] * 2 <= a[i])
            return cout << -1, 0;
        node[i] = node[i - 1] * 2 - a[i];
    }
    for (int i = n - 1; i >= 0; i--) {
        if (node[i + 1] + a[i + 1] < node[i])
            node[i] = node[i + 1] + a[i + 1];
        if (node[i] * 2 < node[i + 1] + a[i + 1])
            return cout << -1, 0;
    }
    for (int i = 0; i <= n; i++)
        res += a[i] + node[i];
    write(res);
}