Problem p02665 (python)

Fixed submission:
depth = int(input()) 
leaves = list(map(int, input().split(" ")))
limited = []
limited_a = []

def solve (depth) :
    cur = 1
    i = 0 
    while i <= depth :
        limited_a.append( cur )
        if cur <= 0 and leaves[i] != 0 :
            return False
        cur=(cur-leaves[i])*2
        i+=1 
    
    _min = _max = 0
    i = depth
    while i >= 0 :
        if i==depth: 
            if leaves[i] > limited_a[i] :
                return False
            limited.append(leaves[i])
            _max = leaves[i]
            _min = (leaves[i]+1)//2
        else :
            _max = min(limited_a[i], _max+leaves[i])
            _min = (_min+leaves[i]+1)//2
            if _max < _min :
                return False 
            limited.append(_max)
        i -= 1
    return True

if solve(depth) :
    print(sum(limited))
else :
    print (-1)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(50000)

depth = int(input()) 
leaves = list(map(int, input().split(" ")))
limited = []

def find( i, depth, cur) :
    if i == depth :
        limited.append(leaves[i])
        return ( leaves[i], (leaves[i]+1)//2 )
    _max, _min = find(i+1, depth, (cur-leaves[i])*2 )
    _max = min(cur, _max+leaves[i])
    _min += leaves[i]
    if _max < _min :
        return (-float('inf'), float('inf') )
    limited.append(_max)
    return ( _max, (_min+1)//2 )

find(0, depth, 1) 
if len(limited) != depth+1 :
    print(-1)
else :
    print(sum(limited))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(12000)

depth = int(input()) 
leaves = list(map(int, input().split(" ")))
limited = []

def find( i, depth, cur) :
    if i == depth :
        limited.append(leaves[i])
        return ( leaves[i], (leaves[i]+1)//2 )
    _max, _min = find(i+1, depth, (cur-leaves[i])*2 )
    _max = min(cur, _max+leaves[i])
    _min += leaves[i]
    if _max < _min :
        return (-float('inf'), float('inf') )
    limited.append(_max)
    return ( _max, (_min+1)//2 )

find(0, depth, 1) 
if len(limited) != depth+1 :
    print(-1)
else :
    print(sum(limited))

Buggy submission (verdict: Runtime Error):
depth = int(input()) 
leaves = list(map(int, input().split(" ")))
limited = []
def find( i, depth, cur) :
    if i == depth :
        limited.append(leaves[i])
        return ( leaves[i], (leaves[i]+1)//2 )
    _max, _min = find(i+1, depth, (cur-leaves[i])*2 )
    _max = min(cur, _max+leaves[i])
    _min += leaves[i]
    if _max < _min :
        return (-float('inf'), float('inf') )
    limited.append(_max)
    return ( _max, (_min+1)//2 )

find(0, depth, 1) 
if len(limited) != depth+1 :
    print(-1)
else :
    print(sum(limited))

Buggy submission (verdict: Runtime Error):
depth = int(input()) 
leaves = list(map(int, input().split(" ")))
limited = []
def find( i, depth) :
    if i == depth :
        limited.append(leaves[i])
        return ( leaves[i], (leaves[i]+1)//2 )
    _max, _min = find(i+1, depth)
    _max = min(pow(2,i), _max+leaves[i])
    _min += leaves[i]
    if _max < _min :
        return (-float('inf'), float('inf') )
    limited.append(_max)
    return ( _max, (_min+1)//2 )

find(0, depth) 
if len(limited) != depth+1 :
    print(-1)
else :
    ans = 0
    cur = 1
    leaves.append(0)
    for i in range(depth+1) :
        ans += min(cur, limited[-i-1])
        cur = (cur-leaves[i])*2  
    
    print(ans)