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;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<VVLL> VVVLL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;

#define SORT_ASC(c) sort((c).begin(), (c).end())
#define SORT_DESC(c) sort((c).begin(), (c).end(), greater<typeof((c).begin())>())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define FORL(i,a,b) for(LL i=(a);i<(b);++i)
#define REPL(i,n)  FORL(i,0,n)
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()

const double EPS = 1e-10;
const double PI  = acos(-1.0);

 //debug func
template<typename T>
void vprint(vector<T> v) {
    for(auto x : v) {
        cerr << x << " ";
    }
    cerr << endl;
}

template<typename T>
void vvprint(vector<vector<T>> vv) {
    REP(i, SIZE(vv)) {
        REP(j, SIZE(vv[i])) {
            cerr << vv[i][j] << " ";
        }
        cerr << endl;
    }
}

template<typename T1, typename T2>
void vpprint(vector<pair<T1, T2>> vp) {
    REP(i, SIZE(vp)) {
        cerr << vp[i].first << ", " << vp[i].second << endl;
    }
}

template<typename T1, typename T2>
void mprint(map<T1, T2> m) {
    for(auto x : m) cerr << x.first << ", " << x.second << endl;
}

template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k, const Iterator last)
{
   /* Credits: Thomas Draper */
   if ((first == last) || (first == k) || (last == k))
      return false;
   Iterator itr1 = first;
   Iterator itr2 = last;
   ++itr1;
   if (last == itr1)
      return false;
   itr1 = last;
   --itr1;
   itr1 = k;
   --itr2;
   while (first != itr1)
   {
      if (*--itr1 < *itr2)
      {
         Iterator j = k;
         while (!(*itr1 < *j)) ++j;
         iter_swap(itr1,j);
         ++itr1;
         ++j;
         itr2 = k;
         rotate(itr1,j,last);
         while (last != j)
         {
            ++j;
            ++itr2;
         }
         rotate(k,itr2,last);
         return true;
      }
   }
   rotate(first,k,last);
   return false;
}

inline double get_time_sec(void){
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count())/1000000000;
}

template<typename T>
T gcd(T a, T b) {
    if(a > b) swap(a, b);
    if(a == 0) return b;
    else return gcd(b%a, a);
}

template<typename T>
T lcm(T a, T b) {
    return (a / gcd(a, b)) * b;
}

template<typename T>
map<T, T> prime_list(T n) {
    map<T, T> ret;
    for(T i = 2; i*i <= n; i++) {
        if(n % i == 0) {
            ret[i] = 0;
            while(n % i == 0) {
                n /= i;
                ret[i]++;
            }
        }
    }
    if(n != 1) ret[n]++;
    return ret;
}

#define MOD 1000000007

LL mypow(LL a, LL n) {
    if(n == 0) return 1;
    if(n == 1) return a % MOD;
    if(n % 2 == 1) return (a * mypow(a, n-1)) % MOD;
    LL t = mypow(a, n/2);
    return (t * t) % MOD;
}

#define FACT_SZ 100010
VLL _fact, _inv;
bool _fact_flg = true;

void _fact_init() {
    _fact = VLL(FACT_SZ);
    _inv = VLL(FACT_SZ);
    _fact[0] = 1;
    FOR(i, 1, FACT_SZ) _fact[i] = (_fact[i-1] * i) % MOD;
    _inv[FACT_SZ-1] = mypow(_fact[FACT_SZ-1], MOD - 2);
    for(int i = FACT_SZ - 2; i >= 0; i--) {
        _inv[i] = ((i + 1) * _inv[i + 1]) % MOD;
    }
}

LL mycomb(LL n, LL k) {
    if(_fact_flg) {
        _fact_flg = false;
        _fact_init();
    }
    if(n < k) return 0;
    return (((_fact[n] * _inv[k]) % MOD) * _inv[n-k]) % MOD;
}

VLL par, rnk, sz;

int root(int x) {
    if(par[x] == x) return x;
    else return par[x] = root(par[x]);
}

bool same(int x, int y) {
    x = root(x);
    y = root(y);
    return x == y;
}

void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if(rnk[x] < rnk[y]) {
        par[x] = y;
        // sz[y] += sz[x];
    } else {
        par[y] = x;
        // sz[x] += sz[y];
        if(rnk[x] == rnk[y]) rnk[x]++;
    }
}

#define INF 1e15

int main(void) {
    LL n;
    cin >> n;
    VLL a(n+1);
    REP(i, n+1) cin >> a[i];

    VLL totalmax(n+1), totalmin(n+1), alivemax(n+1), alivemin(n+1);
    totalmax[0] = 1;
    totalmin[0] = 1;
    alivemax[0] = totalmax[0] - a[0];
    alivemin[0] = totalmin[0] - a[0];
    if(alivemax[0] < 0) {
        cout << -1 << endl;
        return 0;
    }
    FOR(i, 1, n+1) {
        totalmin[i] = alivemin[i-1];
        totalmax[i] = alivemax[i-1] * 2LL;
        alivemin[i] = totalmin[i] - a[i];
        alivemax[i] = totalmax[i] - a[i];
        if(alivemax[i] < 0) {
            cout << -1 << endl;
            return 0;
        }
        alivemin[i] = max(0LL, alivemin[i]);
        alivemax[i] = min(alivemax[i], (LL)1e17);
    }
    if(alivemin[n] > 0) {
        cout << -1 << endl;
        return 0;
    }
    cerr << "===" << endl;
    vprint(totalmax);
    vprint(totalmin);
    cerr << "---" << endl;
    vprint(alivemax);
    vprint(alivemin);

    VLL totalmax2(n+1), totalmin2(n+1), alivemax2(n+1), alivemin2(n+1);
    alivemax2[n] = 0;
    alivemin2[n] = 0;
    totalmax2[n] = alivemax2[n] + a[n];
    totalmin2[n] = alivemin2[n] + a[n];
    for(int i = n-1; i >= 0; i--) {
        alivemax2[i] = totalmax2[i+1];
        alivemin2[i] = (totalmin2[i+1] + 1LL) / 2LL;
        totalmax2[i] = alivemax2[i] + a[i];
        totalmin2[i] = alivemin2[i] + a[i];
    }
    // cerr << "===" << endl;
    // vprint(totalmax2);
    // vprint(totalmin2);
    // cerr << "---" << endl;
    // vprint(alivemax2);
    // vprint(alivemin2);
    LL ans = 0;
    REP(i, n+1) {
        if(totalmin[i] > totalmax2[i] || totalmin2[i] > totalmax[i]) {
            cout << -1 << endl;
            return 0;
        }
        // cerr << min(totalmax[i], totalmax2[i]) << endl;
        ans += min(totalmax[i], totalmax2[i]);
    }
    cout << ans << endl;
}