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 (Python):
import sys, random, math
from bisect import bisect_left, bisect_right
input = sys.stdin.readline

def main():
    inf = 10 ** 20



    t = 1
    
    for _ in range(1, t+1):
    
        
        n = int(input())
        a = [int(x) for x in input().split()]
        
        ans = 0
        node = 1
        sua = sum(a)
        for x in a:
            ans += node
            if(x > node):
                ans = -1
                break
            node -= x
            sua -= x
            node = min(node*2, sua)
        
        
        print(ans)
        
        
main()

Solution (Python):
N, = map(int, input().split())
X = list(map(int, input().split()))
mx = sum(X)

c = 1
R = 0
for i in range(N+1):
    x = X[i]
    R += c
    c -= x
    mx -= x
    if c < 0:
        print(-1)
        break
    c = min(c*2, mx)
else:
    print(R)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
branch = sum(a) - 1
now = 1
ans = 0
for i, num in enumerate(a):
    ans += now
    now -= num
    if branch >= now:
        branch -= now
        now *= 2
    elif branch > 0:
        now += branch
        branch = 0
    if now < 0:
        print(-1)
        exit()
print(ans)

Solution (Python):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
remain = sum(A)
depth = 0
end = 1  
answer = 0
for i, a in enumerate(A):
    answer += end
    if end < a:
        print(-1); exit()
    end -= a
    remain -= a
    end *= 2
    end = min(remain, end)

print(answer)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *A = map(int, read().split())

if N == 0:
    x = A[0]
    if x == 1:
        print(1)
    else:
        print(-1)
    exit()

INF = 1 << 60

bound = [0] * (N + 1)
bound[0] = 1
ok = True
for n in range(1, N + 1):
    bound[n] = min(INF, (bound[n - 1] - A[n - 1]) * 2)
    if bound[n] < A[n]:
        ok = False

if not ok:
    print(-1)
    exit()

B = [0] * (N + 2)
for n in range(N, -1, -1):
    x = min(bound[n], B[n + 1] + A[n])
    B[n] = x

print(sum(B))

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
Can = [0]*n
ans = 0
can = 1
for i,a in enumerate(A):
    if can < a:
        print(-1)
        exit(0)
    elif i < n:
        can -= a
        can *= 2
        Can[i] = can
real = A[-1]
for i in range(n-1,-1,-1):
    ans += real
    real = min(real,Can[i]//2)
    real += A[i]
print(ans + 1)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

S = [0]*(N+1)
for i in range(N)[::-1]:
  S[i] = S[i+1]+A[i+1]

r = 1
ans = 1
for i in range(N+1):
  r -= A[i]
  if i<N and r<=0 or r<0:
    print(-1)
    exit(0)
  r = min(2*r, S[i])
  ans += r
print(ans)

Solution (Python):
n = int(input())
al = list(map(int,input().split()))

sum_a = sum(al)

potential = 1
ans = 0
for a in al:
    ans += min(sum_a, potential)
    sum_a -= a
    potential = 2 * (potential - a)
    if potential < 0:
        print(-1)
        exit()
print(ans)

Solution (Python):
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')


def solve():
    n = ni()
    a = nl()
    ok = -1
    ng = 10**18
    ans = -1
    while ng - ok > 1:
        v = mid = (ok + ng) // 2
        c = 0
        cnt = 0
        for x in a[:0:-1]:
            cnt += c + x
            c += x
            d = (c - 1) // 2 + 1
            if v > c - d:
                v -= c - d
                d = c
            elif v > 0:
                d += v
                v = 0
            c = d
        if a[0] + c > 1:
            ng = mid
        else:
            ok = mid
            ans = cnt + 1
    print(ans)
    return

solve()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

if n == 0:
    if a[0] == 1:
        print(1)
    else:
        print(-1)
else:
    if a[0] == 1:
        print(-1)
    else:
        lefts = [sum(a)]
        for i in range(1, len(a)):
            lefts.append(lefts[-1] - a[i])

        b = [0] * (n + 1)
        b[0] = 1

        for i in range(1, n + 1):
            b[i] = min(b[i-1] * 2 - a[i], lefts[i])

        if min(b) < 0:
            print(-1)
        else:
            print(sum(b) + sum(a))

Solution (Python):
from sys import exit
n = int(input())
a = list(map(int, input().split()))

back = [0 for _ in range(n+1)]
back[n] = a[n]
for i in range(n-1, -1, -1):
	back[i] = back[i+1] + a[i]

ans = 0
v = 1
for i in range(n+1):
	if v < a[i]:
		print(-1)
		exit()
	ans += v
	p = v - a[i]
	if i < n:
		if p > back[i+1]:
			print(-1)
			exit()
		elif p*2 > back[i+1]:
			v = back[i+1]
		else:
			v = 2*p
	else:
		if v != a[i]:
			print(-1)
			exit()
print(ans)