Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import numpy as np
  n=int(input())
  a=list(map(int,input().split()))
  v=np.array([ai+1 for ai in a])
  v[-1]-=1
  vs=np.array([1]*n+[0])
  a[-1]-=1
  # 深さnの一本道の木を初期状態とする。
  # a[i]:深さiの葉でまだ木に連結していない葉
  nozero_vs=0
  for i in range(n+1):
    if a[i]==0:
      continue
    if nozero_vs>=i:# a[0]>0のときもここで処理
      print(-1)
      exit(0)
    # 未連結の葉を木にくっつける
    if vs[nozero_vs]>a[i]:
      vs[nozero_vs]-=a[i]
      vs[nozero_vs+1:i]+=a[i]
      v[nozero_vs+1:i]+=a[i]
    elif vs[nozero_vs]==a[i]:
      vs[nozero_vs]=0
      vs[nozero_vs+1:i]+=a[i]
      v[nozero_vs+1:i]+=a[i]
      nozero_vs+=1
    else:
      sum_vs=0
      while sum_vs<a[i]:
        if nozero_vs>=i:
          print(-1)
          exit(0)
        sum_vs+=vs[nozero_vs]
        if sum_vs<a[i]:
          b=vs[nozero_vs]
          vs[nozero_vs]=0
          vs[nozero_vs+1:i]+=b
          v[nozero_vs+1:i]+=b
          nozero_vs+=1
        else:
          b=sum_vs-a[i]
          c=vs[nozero_vs]-b
          vs[nozero_vs]=b
          vs[nozero_vs+1:i]+=c
          v[nozero_vs+1:i]+=c
  print(np.sum(v))

if __name__=='__main__':
  main()


Fixed submission:
n=int(input())
a=list(map(int,input().split()))
if n==0:
  if a[0]==1:
    print(1)
    exit()
  else:
    print(-1)
    exit()
if a[0]>0:
  print(-1)
  exit()

ans=1
pre_no_ha=1
all_ha=sum(a)

for ai in a[1:]:
  if pre_no_ha<=0:
    print(-1)
    exit()
  if ai>pre_no_ha*2:
    print(-1)
    exit()
  
  pre_no_ha*=2
  pre_no_ha-=ai
  all_ha-=ai
  pre_no_ha=min(pre_no_ha,all_ha)
  ans+=pre_no_ha+ai

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import numpy as np
  n=int(input())
  a=list(map(int,input().split()))
  v=np.array([ai+1 for ai in a])
  v[-1]-=1
  vs=np.array([1]*n+[0])
  a[-1]-=1
  # 深さnの一本道の木を初期状態とする。
  # a[i]:深さiの葉でまだ木に連結していない葉
  nozero_vs=0
  for i in range(n+1):
    if a[i]==0:
      continue
    if nozero_vs>=i:# a[0]>0のときもここで処理
      print(-1)
      exit(0)
    # 未連結の葉を木にくっつける
    if vs[nozero_vs]>a[i]:
      vs[nozero_vs]-=a[i]
      vs[nozero_vs+1:i]+=a[i]
      v[nozero_vs+1:i]+=a[i]
    elif vs[nozero_vs]==a[i]:
      vs[nozero_vs]=0

      vs[nozero_vs+1:i]+=a[i]
      v[nozero_vs+1:i]+=a[i]
      nozero_vs+=1
    else:
      sum_vs=0
      while sum_vs<a[i]:
        if nozero_vs>=i:
          print(-1)
          exit(0)
        sum_vs+=vs[nozero_vs]
        if sum_vs<a[i]:
          b=vs[nozero_vs]
          vs[nozero_vs]=0

          vs[nozero_vs+1:i]+=b
          v[nozero_vs+1:i]+=b
          nozero_vs+=1
        else:
          b=sum_vs-a[i]
          c=vs[nozero_vs]-b
          vs[nozero_vs]=b
          vs[nozero_vs+1:i]+=c
          v[nozero_vs+1:i]+=c
  print(sum(v))

if __name__=='__main__':
  main()


Fixed submission:
n=int(input())
a=list(map(int,input().split()))
if n==0:
  if a[0]==1:
    print(1)
    exit()
  else:
    print(-1)
    exit()
if a[0]>0:
  print(-1)
  exit()

ans=1
pre_no_ha=1
all_ha=sum(a)

for ai in a[1:]:
  if pre_no_ha<=0:
    print(-1)
    exit()
  if ai>pre_no_ha*2:
    print(-1)
    exit()
  
  pre_no_ha*=2
  pre_no_ha-=ai
  all_ha-=ai
  pre_no_ha=min(pre_no_ha,all_ha)
  ans+=pre_no_ha+ai

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n=int(input())
  a=list(map(int,input().split()))
  v=[ai+1 for ai in a]
  v[-1]-=1
  vs=[1]*n+[0]
  a[-1]-=1
  # 深さnの一本道の木を初期状態とする。
  # a[i]:深さiの葉でまだ木に連結していない葉
  nozero_vs=0
  for i in range(n+1):
    if a[i]==0:
      continue
    if nozero_vs>=i:# a[0]>0のときもここで処理
      print(-1)
      exit(0)
    # 未連結の葉を木にくっつける
    if vs[nozero_vs]>a[i]:
      vs[nozero_vs]-=a[i]
      for j in range(nozero_vs+1,i):
        vs[j]+=a[i]
        v[j]+=a[i]
    elif vs[nozero_vs]==a[i]:
      vs[nozero_vs]=0
      for j in range(nozero_vs+1,i):
        vs[j]+=a[i]
        v[j]+=a[i]
      nozero_vs+=1
    else:
      sum_vs=0
      while sum_vs<a[i]:
        if nozero_vs>=i:
          print(-1)
          exit(0)
        sum_vs+=vs[nozero_vs]
        if sum_vs<a[i]:
          b=vs[nozero_vs]
          vs[nozero_vs]=0
          for j in range(nozero_vs+1,i):
            vs[j]+=b
            v[j]+=b
          nozero_vs+=1
        else:
          b=sum_vs-a[i]
          c=vs[nozero_vs]-b
          vs[nozero_vs]=b
          for j in range(nozero_vs+1,i):
            vs[j]+=c
            v[j]+=c
  print(sum(v))

if __name__=='__main__':
  main()


Fixed submission:
n=int(input())
a=list(map(int,input().split()))
if n==0:
  if a[0]==1:
    print(1)
    exit()
  else:
    print(-1)
    exit()
if a[0]>0:
  print(-1)
  exit()

ans=1
pre_no_ha=1
all_ha=sum(a)

for ai in a[1:]:
  if pre_no_ha<=0:
    print(-1)
    exit()
  if ai>pre_no_ha*2:
    print(-1)
    exit()
  
  pre_no_ha*=2
  pre_no_ha-=ai
  all_ha-=ai
  pre_no_ha=min(pre_no_ha,all_ha)
  ans+=pre_no_ha+ai

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n=int(input())
  a=list(map(int,input().split()))
  v=[ai+1 for ai in a]
  v[-1]-=1
  vs=[1]*n+[0]
  a[-1]-=1
  # 深さnの一本道の木を初期状態とする。
  # a[i]:深さiの葉でまだ木に連結していない葉
  nozero_vs=0
  for i in range(n+1):
    if a[i]==0:
      continue
    if nozero_vs>=i:# a[0]>0のときもここで処理
      print(-1)
      exit(0)
    # 未連結の葉を木にくっつける
    if vs[nozero_vs]>a[i]:
      vs[nozero_vs]-=a[i]
      for j in range(nozero_vs+1,i):
        vs[j]+=a[i]
        v[j]+=a[i]
    elif vs[nozero_vs]==a[i]:
      vs[nozero_vs]=0
      for j in range(nozero_vs+1,i):
        vs[j]+=a[i]
        v[j]+=a[i]
      nozero_vs+=1
    else:
      sum_vs=0
      while sum_vs<a[i]:
        if nozero_vs>=i:
          print(-1)
          exit(0)
        sum_vs+=vs[nozero_vs]
        if sum_vs<a[i]:
          b=vs[nozero_vs]
          vs[nozero_vs]=0
          for j in range(nozero_vs+1,i):
            vs[j]+=b
            v[j]+=b
          nozero_vs+=1
        else:
          b=sum_vs-a[i]
          c=vs[nozero_vs]-b
          vs[nozero_vs]=b
          for j in range(nozero_vs+1,i):
            vs[j]+=c
            v[j]+=c
  print(sum(v))

if __name__=='__main__':
  main()


Fixed submission:
n=int(input())
a=list(map(int,input().split()))
if n==0:
  if a[0]==1:
    print(1)
    exit()
  else:
    print(-1)
    exit()
if a[0]>0:
  print(-1)
  exit()

ans=1
pre_no_ha=1
all_ha=sum(a)

for ai in a[1:]:
  if pre_no_ha<=0:
    print(-1)
    exit()
  if ai>pre_no_ha*2:
    print(-1)
    exit()
  
  pre_no_ha*=2
  pre_no_ha-=ai
  all_ha-=ai
  pre_no_ha=min(pre_no_ha,all_ha)
  ans+=pre_no_ha+ai

print(ans)