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



Sample input:
3
0 1 1 2


Sample output:
7

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
node = [0 for i in range(n+1)]
if n == 0:
    if a[0] != 1:
        print(-1)
        exit()
    else:
        print(1)
        exit()
if a[0] != 0:
    print(-1)
    exit()
node[0] = 1
for i in range(1,n+1):
    if a[i] > pow(2, i):
        print(-1)
        exit()
    node[i] = node[i-1] - a[i-1]
    node[i] *= 2
node[-1] = a[-1]
for i in range(n-1, -1, -1):
    if a[i] > node[i]:
        print(-1)
        exit()
    node[i] = min(node[i], node[i+1] + a[i])
print(sum(node))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]

SUM = sum(A) - A[0]
B = 1 - A[0]
ans = 1

for i in range(1, N+1):
    SUM -= A[i]
    res = min(2*B-A[i], SUM)
    if res < 0:
        ans = -1
        break
    ans = ans + res + A[i] 
    B = res
    
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

# それぞれの深さの葉の数を確認
for i in range(N+1):
    if A[i] >= pow(2, i) or A[0] != 0:
        print(-1)
        sys.exit()

B = [0] * (N+1)
B[0] = 1
for j in range(1, N):
    B[j] = min(2*B[j-1] - A[j], sum(A[j+1:]))
print(sum(A)+sum(B))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
s = [0]*(N+2)
for i in range(N + 1):
    s[i+1] = s[i] + A[i]
node, leaf = 1, 1
for d in range(1, N+1):
    leaf = min((leaf - A[d - 1]) * 2, s[N + 1] - s[d])
    if leaf <= 0:
        print(-1)
        break
    node += leaf
else:
    print(node)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))

    if A[0] > 0:
        print(-1)
        exit()
    elif N == 0 and A[0] == 1:
        print(1)
        exit()

    INF = 1 << 60
    bound = [0] * (N+1)
    bound[0] = 1
    flag = True

    for i in range(1, N+1):
        bound[i] = min(INF, (bound[i-1] - A[i-1]) * 2)
        if bound[i] < A[i]:
            flag = False

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

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

    print(sum(B))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

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

# それぞれの深さの葉の数を確認
for i in range(N+1):
    if A[i] >= pow(2, i):
        print(-1)
        sys.exit()

B = [0] * (N+1)
B[0] = 1
for j in range(1, N):
    B[j] = min(2*B[j-1] - A[j], sum(A[j+1:]))
print(A, B)
print(sum(A)+sum(B))

Submission status: Wrong Answer

Solution code:
import sys
import numpy
n=int(input())
a=input()
a=a.split()
a=[int(s)for s in a]
M=1
Ml=list()
ml=list(range(n+1))
Ml.append(M)
Q=0
if a[0]>=2:
    print(-1)
    sys.exit()
for i in range(n):
    M=2*M-a[i+1]
    if M<0:
        print(-1)
        sys.exit()
    M1=M+a[i+1]
    Ml.append(M1)
ml=list(map(lambda x:sum(a[x:]),ml))
Q1=sum(numpy.minimum(Ml,ml))
print(Q1)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

def main():
    n = int(input())
    A = list(map(int, input().split()))

    if n  == 0:
        if A[0] == 1:
            return 1
        else:
            return -1
    elif A[0] != 0:
        return -1
    
    nodes = 0
    bound = list(accumulate(A[::-1]))
    bound.reverse()
    # print('bound: ', bound)
    
    above = [0] * (n+1)
    above[0] = 1
    B = [0] * (n+1)
    for i in range(n+1):
        if i == 0:
            B[i] = 1
        else:
            above[i] = (above[i-1] - A[i-1]) * 2
            if  (i < n and above[i] <= 0) or (i == n and above[i] < 0):
                return -1
            B[i] = min(above[i], bound[i])
    # print('above: ', above)
    # print('B: ', B)
    return sum(B)

print(main())

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

if n == 0:
    print(1 if a[0] == 1 else - 1)
    exit()

if a[0] != 0:
    print(-1)
    exit()

b = list(accumulate(a))
v = 1
cnt = 1
for i in range(n - 1):
    if (v - a[i]) * 2 - a[i + 1] < 0:
        print(-1)
        exit()
    v = min((v - a[i]) * 2, b[-1] - b[i])
    cnt += v
print(cnt + a[-1])

Submission status: Wrong Answer

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

res = 0

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

s = sum(A)
n = 1

res += n
if A[0] != 0:
    print(-1)
    exit()

for i in range(1,N+1):
    current = 0
    if n*2 < s - A[i]:
        current = n*2

    else:
        current = s

    res += current
    s -= A[i]
    n = current -A[i]
    if n < 0:
        print(-1)
        exit()

print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())

a_li = list(map(int, input().split()))

ans = 0
li = [0]*(n+1)

for i,a in enumerate(a_li):
  ans += (i+1)*a

for i,a in enumerate(a_li[::-1]):
  if i >= 1:
    li[i] = li[i-1]+a
  else:
    li[i] = a

li = li[::-1]

#print("-----")
#print(ans)
#print(li)
#print("-----")

flag = True
ne = 0.5
for i in range(n+1):
  if li[i]  > 2*ne:
    ans -= li[i] -2*ne
  
  ne = min(li[i] - a_li[i],2*ne-a_li[i])
  #print(ne)

  if (i == n and ne != 0) or (i != n and ne <= 0):
    flag = False

  
if flag:
  print(int(ans))
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
last = A[-1]
count = last
root = 1
for i in range(N):
  if root <= A[i]:
    print(-1)
    break
  if i+1 == N and root-A[i] > A[i+1]:
    root = last + A[i]
  elif root-A[i] > (last + A[i+1]):
    root = (last + A[i+1]) + A[i]
  count+=root
  root = (root-A[i])*2
else:
  if count == 0 or root < last:
    count = -1
  print(count)

Submission status: Wrong Answer

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

if n == 0:
    if a[0] != 0:
        print(-1)
        exit()
    if a[1] > 2:
        print(-1)
        exit()
    else:
        print(1 + sum(a))
else:
    if a[0] != 0:
        print(-1)
        exit()
    v = [1] * (n + 1)

    for i in range(1, n + 1):
        if i != n:
            if v[i - 1] * 2 < a[i]:
                print(-1)
                exit()
            else:
                v[i] = v[i - 1] * 2 - a[i]
        else:
            if v[i - 1] * 2 < a[i]:
                print(-1)
                exit()
            else:
                v[i] = a[i]
    
    ans = 0
    sum_v = 0
    for i in range(1, n + 1):
        sum_v += a[-i]
        ans += min(v[-i - 1], sum_v)
        sum_v = min(v[-i - 1], sum_v)
    
    print(ans + sum(a))

Submission status: Wrong Answer

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

now_vertex = 1
ans = 0
flag = True
amari = [0] * (n+1)
for i in range(0, n + 1):
    if ((i != n) and (now_vertex is a[i])) or (now_vertex < a[i]):
        ans = -1
        flag = False
        break
    amari[i] = now_vertex - a[i]
    now_vertex = (now_vertex - a[i]) * 2

# vertex_max = list()
# for i in reversed(range(0, n + 1)):
#     if i == n:
#         vertex_max[i] = a[i]
#     else:
#         vertex_max[i] = (a[i] + vertex_max[i + 1]) * 2)

vertex_count = [0] * (n+1)
ans = 0
for i in reversed(range(0, n + 1)):
    if flag is False:
        break
    if i == n:
        vertex_count[i] = a[i]
    else:
        vertex_count[i] = a[i] + min(vertex_count[i + 1], amari[i])
    ans += vertex_count[i]

print(ans)

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
A = list(map(int, input().split()))
sums = [0]
branch = [0] * (N+1)
branch[0] = 1
for i in range(len(A)):
    sums.append(sums[-1] + A[i])

flag = True
ans = 1
for i in range(1, N+1):
    ma = min((branch[i-1] - A[i-1])*2, sums[-1]-sums[i])
    ans += ma
    if ma < 1:
        flag = False
        break
    branch[i] = ma

if flag:
    print(sum(branch))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))

    if A[0] > 0:
        print(-1)
        exit()
    elif N == 0 and A[0] == 1:
        print(1)
        exit()

    bound = [0] * (N+1)
    bound[0] = 1
    flag = True

    for i in range(1, N+1):
        bound[i] = (bound[i-1] - A[i-1]) * 2
        if bound[i] < A[i]:
            flag = False

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

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

    print(sum(B))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
V = [0]*(N+1)
flug = 0
Lsum= sum(A)

V[0] = 1
for i in range(1,N):
    V[i] = (V[i-1]*2 - A[i])
    if V[i] <= 0:
        flug = 1
    Lsum = Lsum - A[i]
    if V[i] > Lsum :
        V[i] = Lsum
V[N]=0
#print(V)
 
ans = sum(V)+sum(A)
if A[0] != 0:
    ans = -1
if flug ==1:
   ans = -1
if N == 0:
    ans = -1

print(ans)

Submission status: Wrong Answer

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

now_vertex = 1
ans = 0
flag = True
amari = [0] * (n+1)
for i in range(0, n + 1):
    if ((i != n) and now_vertex) or (now_vertex < a[i]):
        ans = -1
        flag = False
        break
    amari[i] = now_vertex - a[i]
    now_vertex = (now_vertex - a[i]) * 2

# vertex_max = list()
# for i in reversed(range(0, n + 1)):
#     if i == n:
#         vertex_max[i] = a[i]
#     else:
#         vertex_max[i] = (a[i] + vertex_max[i + 1]) * 2)

vertex_count = [0] * (n+1)
ans = 0
for i in reversed(range(0, n + 1)):
    if flag is False:
        break
    if i == n:
        vertex_count[i] = a[i]
    else:
        vertex_count[i] = a[i] + min(vertex_count[i + 1], amari[i])
    ans += vertex_count[i]

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

if a[0] != 0:
    print(-1)
    exit()

b = list(accumulate(a))
v = 1
cnt = 1
for i in range(n - 1):
    if (v - a[i]) * 2 - a[i + 1] < 0:
        print(-1)
        exit()
    v = min((v - a[i]) * 2, b[-1] - b[i])
    cnt += v
print(cnt + a[-1])

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
k=l[n]
count=1
sumleaf=1
flag=True
i0=0
suml=[]
for i in l:
    i0+=i
    suml.append(i0)
if l[0]>2 or l[0]==1 and n>=1:
    print(-1)
    flag=False

    
else:
    for i in range(1,n+1):
        sumleaf*=2
        if i==n and sumleaf<k:
            flag=False
            print(-1)
            break
        elif i==n:
            count+=k
        else:
            if sumleaf<=l[i]:
                flag=False
                print(-1)
                break
            else:
                sumlnew=suml[n]-suml[i-1]
                count+=min(sumlnew,sumleaf)
                sumleaf=min(sumlnew,sumleaf)-l[i]
    
                    
                    
                    
                    
                
if flag==True:
    print(count)

Submission status: Wrong Answer

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------

N = int(input())
Ha = list( map(int, input().split()) )


#!  方針
#!    1．葉の枚数が可能であるかチェック
#!    2．節の最大数、上から降りて、下から上がる



Fushi=[1]
for i in range(N+1):
  if i==0: continue
  f = Fushi[i-1] * 2
  h = Ha[i]

  a = f-h
  if (a<0) or (a==0 and i==N):
    print(-1)
    exit(0)

  Fushi.append( a )


Fushi[N]=0

#p("H, ",Ha)
#p("F, ", Fushi)


for i in reversed(range(N)):
  #print(i)

  h = Ha[i+1]
  f = Fushi[i+1]

  a = min(Fushi[i], h+f)
  if a<=0:
    print(-1)
    exit(0)

  Fushi[i] = a



#p("F, ", Fushi)
#p("sumH" , sum(Ha))
#p("sumF", sum(Fushi))


ans = sum(Ha) + sum(Fushi)
print(ans)

Submission status: Wrong Answer

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

now_vertex = 1
ans = 0
flag = True
amari = [0] * (n+1)
for i in range(0, n + 1):
    if ((i != n) and now_vertex) or (now_vertex < a[i]):
        ans = -1
        flag = False
        break
    amari[i] = now_vertex - a[i]
    now_vertex = (now_vertex - a[i]) * 2

# vertex_max = list()
# for i in reversed(range(0, n + 1)):
#     if i == n:
#         vertex_max[i] = a[i]
#     else:
#         vertex_max[i] = (a[i] + vertex_max[i + 1]) * 2)

vertex_count = [0] * (n+1)
ans = 0
for i in reversed(range(0, n + 1)):
    if i == n:
        vertex_count[i] = a[i]
    else:
        vertex_count[i] = a[i] + min(vertex_count[i + 1], amari[i])
    ans += vertex_count[i]

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
V = [0]*(N+1)
flug = 0

Lsum= sum(A)

V[0] = 1
for i in range(1,N):
    V[i] = (V[i-1]*2 - A[i])
    if V[i] <= 0:
        flug = 1
    Lsum = Lsum - A[i]
    if V[i] > Lsum :
        V[i] = Lsum
V[N]=0
#print(V)
 
ans = sum(V)+sum(A)
if A[0] != 0:
    ans = -1
if flug ==1:
   ans = -1

print(ans)

Submission status: Wrong Answer

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

if n == 0:
    print(sum(a) + 1)
else:
    if a[0] != 0:
        print(-1)
        exit()
    v = [1] * (n + 1)

    for i in range(1, n + 1):
        if i != n:
            if v[i - 1] * 2 < a[i]:
                print(-1)
                exit()
            else:
                v[i] = v[i - 1] * 2 - a[i]
        else:
            if v[i - 1] * 2 < a[i]:
                print(-1)
                exit()
            else:
                v[i] = a[i]
    
    ans = 0
    sum_v = 0
    for i in range(1, n + 1):
        sum_v += a[-i]
        ans += min(v[-i - 1], sum_v)
        sum_v = min(v[-i - 1], sum_v)
    
    print(ans + sum(a))

Submission status: Wrong Answer

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

node=1
S=[0]*(N+1)
S[0]=1
for i in range(1,N+1):
    leaf=A[i-1]
    node = S[i-1]
    if node>leaf:
        r = node-leaf
        S[i]=(node-leaf)*2
    elif node==leaf:
        if i==N:
            S[i]=leaf
        else:
            print(-1)
            exit()
    else:
        print(-1)
        exit()
#print(S)


ans=1
cur_node=0
for i in reversed(range(1,N+1)):
    leaf = A[i]
    ans += cur_node+leaf

    upper_layer_node = S[i-1]-A[i-1]
    cur_node = min(upper_layer_node,leaf+cur_node)
print(ans)

Submission status: Wrong Answer

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

now_vertex = 1
ans = 0
flag = True
amari = [0] * (n+1)
for i in range(0, n + 1):
    if i != n and now_vertex or now_vertex < a[i]:
        ans = -1
        flag = False
        break
    amari[i] = now_vertex - a[i]
    now_vertex = (now_vertex - a[i]) * 2

# vertex_max = list()
# for i in reversed(range(0, n + 1)):
#     if i == n:
#         vertex_max[i] = a[i]
#     else:
#         vertex_max[i] = (a[i] + vertex_max[i + 1]) * 2)

vertex_count = [0] * (n+1)
ans = 0
for i in reversed(range(0, n + 1)):
    if i == n:
        vertex_count[i] = a[i]
    else:
        vertex_count[i] = a[i] + min(vertex_count[i + 1], amari[i])
    ans += vertex_count[i]

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
k=l[n]
count=1
sumleaf=1
flag=True
i0=0
suml=[]
for i in l:
    i0+=i
    suml.append(i0)
print(suml)
if l[0]>2 or l[0]==1 and n>=1:
    print(-1)
    flag=False

    
else:
    for i in range(1,n+1):
        sumleaf*=2
        if i==n and sumleaf<k:
            flag=False
            print(-1)
            break
        elif i==n:
            count+=k
        else:
            if sumleaf<=l[i]:
                flag=False
                print(-1)
                break
            else:
                sumlnew=suml[n]-suml[i-1]
                count+=min(sumlnew,sumleaf)
                sumleaf=min(sumlnew,sumleaf)-l[i]
    
                    
                    
                    
                    
                
if flag==True:
    print(count)

Submission status: Wrong Answer

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

now_vertex = 1
ans = 0
flag = True
amari = [0] * (n+1)
for i in range(0, n + 1):
    if i != n & now_vertex | now_vertex < a[i]:
        ans = -1
        flag = False
        break
    amari[i] = now_vertex - a[i]
    now_vertex = (now_vertex - a[i]) * 2

# vertex_max = list()
# for i in reversed(range(0, n + 1)):
#     if i == n:
#         vertex_max[i] = a[i]
#     else:
#         vertex_max[i] = (a[i] + vertex_max[i + 1]) * 2)

vertex_count = [0] * (n+1)
ans = 0
for i in reversed(range(0, n + 1)):
    if i == n:
        vertex_count[i] = a[i]
    else:
        vertex_count[i] = a[i] + min(vertex_count[i + 1], amari[i])
    ans += vertex_count[i]

print(ans)

Submission status: Wrong Answer

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

if n == 0:
    print(-1)
else:
    if a[0] != 0:
        print(-1)
        exit()
    v = [1] * (n + 1)

    for i in range(1, n + 1):
        if i != n:
            if v[i - 1] * 2 < a[i]:
                print(-1)
                exit()
            else:
                v[i] = v[i - 1] * 2 - a[i]
        else:
            if v[i - 1] * 2 < a[i]:
                print(-1)
                exit()
            else:
                v[i] = a[i]
    
    ans = 0
    sum_v = 0
    for i in range(1, n + 1):
        sum_v += a[-i]
        ans += min(v[-i - 1], sum_v)
        sum_v = min(v[-i - 1], sum_v)
    
    print(ans + sum(a))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

def main():
    n = int(input())
    A = list(map(int, input().split()))

    if n  == 0:
        if A[0] == 1:
            return 1
        else:
            return -1
    elif A[0] != 0:
        return -1
    
    nodes = 0
    bound = list(accumulate(A[::-1]))
    bound.reverse()
    # print('bound: ', bound)
    
    above = [0] * (n+1)
    above[0] = 1
    B = [0] * (n+1)
    for i in range(n+1):
        if i == 0:
            B[i] = 1
        else:
            above[i] = (above[i-1] - A[i-1]) * 2
            if  (i < n and above[i] <= 0) or above[i] < 0:
                return -1
            B[i] = min(above[i], bound[i])
    # print('above: ', above)
    # print('B: ', B)
    return sum(B)

print(main())

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
bottom = sum(A)

if A[0] >= 1 and N != 0:
  print(-1)
  exit()
 
ret = 1
children = 1 - A[0]
bottom -= A[0]
 
for i in range(N):
    children = children * 2 - A[i+1]
    if children <= -1:
      ret = -1
      break
    
    bottom -= A[i+1]
    if children >= bottom:
      children = bottom
    ret += children + A[i+1]
print(ret)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
 
ans = 1
prev = 1
L = [0]*(N+1)
L[0] = 1
for i in range(1, N+1):
    if prev*2 > A[i]:
        prev = prev*2-A[i]
    elif prev*2 == A[i] and i == N:
        continue
    else:
        ans = -1
        break
    L[i] = prev
    if prev > 10**10:
        for j in range(i+1, N+1):
            L[j] = "inf"
        break

if A[0] > 0:
    ans = -1
    if A[0] == 1 and N == 0:
        ans = 1

if ans == 1:
    num = 0
    for i in range(N, 0, -1):
        if L[i] == "inf" or num+A[i] <= 2*L[i-1]:
            num += A[i]
        else:
            num = 2*L[i-1]
        ans += num
print(ans)

Submission status: Wrong Answer

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

if a[0] != 0:
    print(-1)
    exit()
v = [1] * (n + 1)

for i in range(1, n + 1):
    if i != n:
        if v[i - 1] * 2 < a[i]:
            print(-1)
            exit()
        else:
            v[i] = v[i - 1] * 2 - a[i]
    else:
        if v[i - 1] * 2 < a[i]:
            print(-1)
            exit()
        else:
            v[i] = a[i]

ans = 0
sum_v = 0
for i in range(1, n + 1):
    sum_v += a[-i]
    ans += min(v[-i - 1], sum_v)
    sum_v = min(v[-i - 1], sum_v)

print(ans + sum(a))

Submission status: Wrong Answer

Solution code:
print(1)

Submission status: Wrong Answer

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

if N == 1 and A[0] == 1:
  print(1)
else:
  print(-2)

Submission status: Wrong Answer

Solution code:
n= int(input())

a = list(map(int, input().split()))

if n == 0 and a[0]==1:
    print(1)
    exit(0)

if n == 0:
    print(-1)
    exit(0)


if a[0] != 0:
    print(-1)
    exit(0)


b=1
count=1
asum =sum(a)
alim = asum

for i in range(n-1):
    if a[i+1] > 2*b:
        print(-1)
        exit(0)
    else:
        alim = alim - a[i+1]
        b= min(2*b-a[i+1], alim)

        count += b



print(count+ asum)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n = int(input())
As = list(map(int, input().split()))

if n == 0:
    if As[0] == 1:print(1)
    else:print(-1)
    exit()


if As[0] != 0:
    print(-1)
    exit()

b = 1
ans = 1
acc = list(accumulate(As))
s = acc[-1]
for ac,a in zip(acc[1:], As[1:]):
    print(s-ac,2*b-a)
    b = min(2*b-a, s-ac)
    if b < 0:
        print(-1)
        exit()
    ans += b+a
print(ans)

Submission status: Wrong Answer

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

if a[0] != 0:
    print(-1)
    exit()
v = [1] * (n + 1)

for i in range(1, n + 1):
    if i != n:
        if v[i - 1] * 2 <= a[i]:
            print(-1)
            exit()
        else:
            v[i] = v[i - 1] * 2 - a[i]
    else:
        if v[i - 1] * 2 < a[i]:
            print(-1)
            exit()
        else:
            v[i] = a[i]

ans = 0
sum_v = 0
for i in range(1, n + 1):
    sum_v += a[-i]
    ans += min(v[-i - 1], sum_v)
    sum_v = min(v[-i - 1], sum_v)

print(ans + sum(a))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]"):
    if N == 0:
        if A == [0]:
            print('0')
        elif A == [1]:
            print('1')
        else:
            print('-1')
        return

    if N == 1:
        if A == [0, 1]:
            print('2')
        elif A == [0, 2]:
            print('3')
        else:
            print('-1')
        return

    if A[0] != 0:
        print('-1')
        return

    f = [0 for i in range(N)]

    f[0] = 1
    count = sum(A) - 1

    for d in range(1, N):
        f[d] = 2 * min(f[d - 1], count) - A[d] + min(max(f[d - 1] - count, 0), f[d - 1])
        count -= min(f[d - 1], count)
        if f[d] <= 0:
            print('-1')
            return
    print(sum(f) + sum(A))
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N - 0 + 1)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
V = [0]*(N+1)
flug = 0

V[0] = 1
for i in range(1,N):
    V[i] = (V[i-1]*2 - A[i])
    if V[i] <= 0:
        flug = 1
V[N]=0
#print(V)

for i in range(N):
    j = N - i -1
    if V[j] > A[j+1] + V[j+1]:
        V[j] = A[j+1] + V[j+1]
 
ans = sum(V)+sum(A)

if A[0] != 0:
    ans = -1

if flug ==1:
   ans = -1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy
n=int(input())
a=input()
a=a.split()
a=[int(s)for s in a]
M=1
m=int(a[n])
Ml=list()
ml=list(range(n+1))
Ml.append(M)
Q=0
if a[0]>=2:
    print(-1)
    sys.exit()
for i in range(n):
    M=2*M-a[i+1]
    if M<0:
        print(-1)
        sys.exit()
    M1=M+a[i+1]
    Ml.append(M1)
ml=map(lambda x:sum(a[x:]),ml)
ml=list(ml)
Q1=numpy.minimum(Ml,ml)
Q1=sum(Q1)
print(Q1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
last = A[-1]
count = last
root = 1
for i in range(N):
  if root <= A[i]:
    print(-1)
    break
  if i+1 == N and root-A[i] > A[i+1]:
    root = last + A[i]
  elif root-A[i] > (last + A[i+1]):
    root = (last + A[i+1]) + A[i]
  count+=root
  root = (root-A[i])*2
else:
  print(count if count != 0 else -1)

Submission status: Wrong Answer

Solution code:
n= int(input())

a = list(map(int, input().split()))

if n == 0 and a[0]==1:
    print(1)
    exit(0)


if a[0] != 0:
    print(-1)
    exit(0)


b=1
count=1
asum =sum(a)
alim = asum

for i in range(n-1):
    if a[i+1] > 2*b:
        print(-1)
        exit(0)
    else:
        alim = alim - a[i+1]
        b= min(2*b-a[i+1], alim)

        count += b



print(count+ asum)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
cir = 1
bsum = 1
hantei=0
asum = [0 for i in range(n+1)]
asum[0] = sum(a)
for i in range(1,n+1):
    asum[i] = asum[i-1]-a[i]
#print(asum)
swi = 0
for i in range(n):
    if swi == 1:
        #print(cir,a[i])
        cir = cir-a[i]
    if swi==0:
        if (cir-a[i])*2 < asum[i]:
            cir = (cir-a[i])*2
        else:
            cir = asum[i]
            swi = 1

    #print(cir)
    bsum += cir
    if cir<=0:
        hantei=1
        break

if hantei==1:
    print(-1)
else:
    print(bsum)

Submission status: Wrong Answer

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

res = 0

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

s = sum(A)
n = 1

res += n
if A[0] != 0:
    print(-1)
    exit()

for i in range(1,N):
    if n < 1 :
        print(-1)
        exit()
    current = 0
    if n*2 < s:
        current = n*2

    else:
        current = s

    res += current
    s -= A[i]
    n = current -A[i]

res += s

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
bottom = sum(A)

if A[0] >= 1 and N != 1:
  print(-1)
  exit()

ret = 1
children = 1 - A[0]
bottom -= A[0]
 
for i in range(N):
    children = children * 2 - A[i+1]
    if children <= -1:
      ret = -1
      break
    
    bottom -= A[i+1]
    if children >= bottom:
      children = bottom
    ret += children + A[i+1]
print(ret)

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    A = [int(a) for a in input().split()]
    low, high = A[N], A[N]
    maxN = [0] * (N + 1)
    maxN[N] = A[N]
    Limit = [2 ** 50] * (N + 1)
    Limit[N] = A[N]

    for i in reversed(range(N)):
        leaf = A[i]
        Limit[i] = min(Limit[i], Limit[i+1] + leaf)
        if i < 50: Limit[i] = min(pow(2, i), Limit[i])
        low = (low + 1) // 2 + leaf
        high += leaf
        if low > Limit[i]:
            print(-1)
            break
        else: 
            maxN[i] = min(high, Limit[i])
    else:
        #print(maxN)
        if N == 0 and A[0] > 0: 
            print(-1)
            return 0
        for i in range(N):
            if (maxN[i] - A[i]) * 2 <= maxN[i+1]: 
                maxN[i + 1] = (maxN[i] - A[i]) * 2
        print(sum(maxN))
        #print(maxN)
        #print(lowN)
            
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
if A[0]>0:
    print(-1)
    exit()
if A[-1]>2**n:
    print(-1)
    exit()
node1=[0]*(n+1)
node1[0]=1
node2=[0]*(n+1)
node2[-1]=A[-1]
for i in range(n):
    x=(node1[i]-A[i])*2
    if x<1:
        print(-1)
        exit()
    node1[i+1]=x
#print(node1)

for i in range(n-1,-1,-1):
    x=(node2[i+1]+A[i])
    if i!=0 and x<A[i+1]:
        print(-1)
        exit()
    node2[i]=x
ans=[]

for i in range(n+1):
    ans.append(min(node1[i],node2[i]))
print(sum(ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
if A[0]>0:
    print(-1)
    exit()
if A[-1]>2**n:
    print(-1)
    exit()
node1=[0]*(n+1)
node1[0]=1
node2=[0]*(n+1)
node2[-1]=A[-1]
for i in range(n):
    x=(node1[i]-A[i])*2
    if x<1:
        print(-1)
        exit()
    node1[i+1]=x
#print(node1)

for i in range(n-1,-1,-1):
    x=(node2[i+1]+A[i])
    if i!=0 and x<A[i-1]:
        print(-1)
        exit()
    node2[i]=x
ans=[]

for i in range(n+1):
    ans.append(min(node1[i],node2[i]))
print(sum(ans))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
if a[0]>=2:
    print(-1)

else:
    pN=1
    s=1
    l=[]
    S=sum(a)
    b=0
    z=1
    for k in range(len(a)):
        b+=a[k]
        l.append(b)

    for i in range(1,n):
        if a[i]>2*pN:
            print(-1)
            z=0
            break
        else:
            pN=min(2*pN-a[i],S-l[i])
            s+=pN
    s+=S
    if n==0:
        s=1
    if a[0]==1 and n>=1:
            print(-1)
            z=0

    if z:
        print(s)