Buggy python submission (verdict: Time Limit Exceeded):
from math import ceil

def a():
    N = int(input())
    A = list(map(int,input().split()))
    INF = 10**13
    if N == 0:
        if A[0] == 1:
            return 1
        else:
            return -1

    BMAX = [INF] * (N+1)
    BMIN = [1] * (N+1)

    a = 1
    for i in range(N+1):
        BMAX[i] = min(BMAX[i] , a - A[i])
        a = BMAX[i] * 2
    BMAX[N] = 0
    BMIN[0] = 1
    BMIN[N] = 0

    for i in range(N-1,-1,-1):
        BMAX[i] = min(BMAX[i],BMAX[i+1]+A[i+1])
        BMIN[i] = max(BMIN[i],ceil((BMIN[i+1]+A[i+1])/2))

    for i in range(1,N+1):
        BMAX[i] = min(BMAX[i],BMAX[i-1]*2-A[i])
        BMIN[i] = max(BMIN[i],BMIN[i-1]-A[i])

    ans = 0
    for i in range(0,N+1):
        if BMIN[i] > BMAX[i]:
            return -1
        ans += (BMAX[i] + A[i])
    return ans

print(a())

Fixed submission:
from math import ceil

def a():
    N = int(input())
    A = list(map(int,input().split()))
    INF = 10**13
    if N == 0:
        if A[0] == 1:
            return 1
        else:
            return -1

    BMAX = [INF] * (N+1)
    BMIN = [1] * (N+1)

    BMAX[0] = 1 - A[0]
    BMAX[N] = 0
    BMIN[0] = 1
    BMIN[N] = 0

    for i in range(N-1,-1,-1):
        BMAX[i] = min(BMAX[i],BMAX[i+1]+A[i+1])
        BMIN[i] = max(BMIN[i],ceil((BMIN[i+1]+A[i+1])/2))

    if BMIN[0] > BMAX[0]:
        return -1

    for i in range(1,N+1):
        BMAX[i] = min(BMAX[i],BMAX[i-1]*2-A[i])
        BMIN[i] = max(BMIN[i],BMIN[i-1]-A[i])
        if BMIN[i] > BMAX[i]:
            return -1
        
    return sum(BMAX)+sum(A)

print(a())

Buggy python submission (verdict: Time Limit Exceeded):
from math import ceil

def a():
    N = int(input())
    A = list(map(int,input().split()))
    if N == 0:
        if A[0] == 1:
            return 1
        else:
            return -1

    BMAX = [10**13] * (N+1)
    BMIN = [1] * (N+1)

    a = 1
    for i in range(N+1):
        BMAX[i] = min(BMAX[i] , a - A[i])
        a = BMAX[i] * 2
    BMAX[N] = 0
    BMIN[0] = 1
    BMIN[N] = 0

    for i in range(N-1,-1,-1):
        BMAX[i] = min(BMAX[i],BMAX[i+1]+A[i+1])
        BMIN[i] = max(BMIN[i],ceil((BMIN[i+1]+A[i+1])/2))

    for i in range(1,N+1):
        BMAX[i] = min(BMAX[i],BMAX[i-1]*2-A[i])
        BMIN[i] = max(BMIN[i],BMIN[i-1]-A[i])

    ans = 0
    for i in range(0,N+1):
        if BMIN[i] > BMAX[i]:
            return -1
        ans += (BMAX[i] + A[i])
    return ans

print(a())

Fixed submission:
from math import ceil

def a():
    N = int(input())
    A = list(map(int,input().split()))
    INF = 10**13
    if N == 0:
        if A[0] == 1:
            return 1
        else:
            return -1

    BMAX = [INF] * (N+1)
    BMIN = [1] * (N+1)

    BMAX[0] = 1 - A[0]
    BMAX[N] = 0
    BMIN[0] = 1
    BMIN[N] = 0

    for i in range(N-1,-1,-1):
        BMAX[i] = min(BMAX[i],BMAX[i+1]+A[i+1])
        BMIN[i] = max(BMIN[i],ceil((BMIN[i+1]+A[i+1])/2))

    if BMIN[0] > BMAX[0]:
        return -1

    for i in range(1,N+1):
        BMAX[i] = min(BMAX[i],BMAX[i-1]*2-A[i])
        BMIN[i] = max(BMIN[i],BMIN[i-1]-A[i])
        if BMIN[i] > BMAX[i]:
            return -1
        
    return sum(BMAX)+sum(A)

print(a())