qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n = int(input())
monsters = list(map(int, input().split()))
heros = list(map(int, input().split()))

killed = 0
attack = 0
attack_l = [heros[0], 0]
for i in range(0, n+1):
  if i != n and i != 0:
    attack_l[1] = heros[i]
  attack = attack_l[0] + attack_l[1]
  if (monsters[i] - attack) < 0:
    killed += monsters[i]
    if (attack_l[0] - monsters[i]) < 0:
      attack_l[1] -= monsters[i] - attack_l[0]
    attack_l[0] = attack_l[1]
    attack_l[1] = 0
  elif (monsters[i] - attack) > 0:
    killed += attack
    attack_l[0] = 0
    attack_l[1] = 0
  elif (monsters[i] - attack) == 0:
    killed += monsters[i]
    attack_l[0] = 0
    attack_l[1] = 0

print(killed)

Fixed code:
n = int(input())
monsters = list(map(int, input().split()))
heros = list(map(int, input().split()))

killed = 0
attack = 0
attack_l = [0, heros[0]]
for i in range(0, n+1):
  if i != n and i != 0:
    attack_l[1] = heros[i]
  attack = attack_l[0] + attack_l[1]
  if (monsters[i] - attack) < 0:
    killed += monsters[i]
    if (attack_l[0] - monsters[i]) < 0:
      attack_l[1] += (attack_l[0] - monsters[i])
    attack_l[0] = attack_l[1]
    attack_l[1] = 0
  elif (monsters[i] - attack) > 0:
    killed += attack
    attack_l[0] = 0
    attack_l[1] = 0
  elif (monsters[i] - attack) == 0:
    killed += monsters[i]
    attack_l[0] = 0
    attack_l[1] = 0

print(killed)
Buggy code:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
B.append(0)

ans = 0
for i in range(N+1):
  taosukazu1 = min(A[i],B[i])
  B[i] -= taosukazu1
  A[i] -= taosukazu1
  ans += taosukazu1
  print(A)
  print(B)
  if B[i] > 0:
    taosukazu2 = min(A[i+1],B[i])
    B[i] -= taosukazu2
    A[i] -= taosukazu2
    ans += taosukazu2
print(ans)

Fixed code:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
B.append(0)

ans = 0
for i in range(N+1):
  taosukazu1 = min(A[i],B[i])
  B[i] -= taosukazu1
  A[i] -= taosukazu1
  ans += taosukazu1
  if B[i] > 0:
    taosukazu2 = min(A[i+1],B[i])
    B[i] -= taosukazu2
    A[i+1] -= taosukazu2
    ans += taosukazu2
print(ans)
Buggy code:
n = int(input())
a = list(map(int, input()))
b = list(map(int, input()))
monster = 0
for i in range(n):
  if b[i] <= a[i]:
    monster += b[i]
  elif b[i] <= a[i] + a[i+1]:
    monster += b[i]
    a[i+1] -= b[i] - a[i]
  else:
    monster += a[i] + a[i+1]
    a[i+1] = 0
print(monster)

Fixed code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
monster = 0
for i in range(n):
  if b[i] <= a[i]:
    monster += b[i]
  elif b[i] <= a[i] + a[i+1]:
    monster += b[i]
    a[i+1] -= b[i] - a[i]
  else:
    monster += a[i] + a[i+1]
    a[i+1] = 0
print(monster)
Buggy code:
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]

monster = 0
for i in range(N):
    if A[i] >= B[i]:
        A[i]=A[i]-B[i]
        monster += B[i]
    else:
        monster += A[i]
        A[i]=0
        if A[i+1]>(B[i]-A[i]):
            monster += (B[i]-A[i])
            A[i+1] = A[i+1] - (B[i]-A[i])
        else:
            monster+=A[i+1]
            A[i+1]=0

print(monster)

Fixed code:
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]

monster = 0
for i in range(N):
    if A[i] >= B[i]:
        A[i]=A[i]-B[i]
        monster += B[i]
    else:
        monster += A[i]
        remain = B[i]-A[i]
        A[i]=0
        if A[i+1]>remain:
            monster += remain
            A[i+1] -= remain
        else:
            monster+=A[i+1]
            A[i+1]=0
print(monster)
Buggy code:
n = int(input())
a_lst = list(map(int,input().split()))
b_lst = list(map(int,input().split()))
monster = 0

for i in range(n):
    a1 = a_lst[i]
    a2 = a_lst[i+1]
    b1 = b_lst[i]
    tmp = b1-a1

    monster += min(a1,b1)
    if tmp>0:
        if a2-tmp<=0:
            monster += a2
            a_lst[i+1] = 0
        else:
            monster += tmp
            a_lst[i+1] -= tmp

print(monster)
print(a_lst)

Fixed code:
n = int(input())
a_lst = list(map(int,input().split()))
b_lst = list(map(int,input().split()))
monster = 0

for i in range(n):
    a1 = a_lst[i]
    a2 = a_lst[i+1]
    b1 = b_lst[i]
    tmp = b1-a1

    monster += min(a1,b1)
    if tmp>0:
        if a2-tmp<=0:
            monster += a2
            a_lst[i+1] = 0
        else:
            monster += tmp
            a_lst[i+1] -= tmp

print(monster)
Buggy code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
sum=0
for i in range(N):
    if A[i] <= B[i]:
        sum += A[i]
        B[i] -= A[i]
    if B[i] > 0:
        if A[i+1] <= B[i]:
            sum += A[i+1]
            A[i+1] =0
        else:
            sum += B[i]
            A[i+1] -= B[i]

print(sum)

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

defeated = 0

for i in range(N):
    if B[i] >= A[i]:
        B[i] -= A[i]
        defeated += A[i]
        
        if B[i] <= A[i+1]:
            A[i+1] -= B[i]
            defeated += B[i]
        else:
            B[i] -= A[i+1]
            defeated += A[i+1]
            A[i+1] = 0
    else:
        A[i] -= B[i]
        defeated += B[i]
        B[i] = 0
        
print(defeated)
Buggy code:
#n = int(input())
#L = list(map(int, input().split()))
import math
import sys
import heapq
import fractions
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0

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

print(ans)

Fixed code:
#n = int(input())
#L = list(map(int, input().split()))
import math
import sys
import heapq
import fractions
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0

for i in range(N):
    if B[i]>A[i]:
        if B[i] >= A[i+1]+A[i]:
            ans += A[i]+A[i+1]
            A[i+1] = 0
        else:
            A[i+1] -= B[i]-A[i]
            ans += B[i]
    else:
        ans += B[i]

print(ans)
Buggy code:
#! /usr/bin/env python3
# -*- coding:utf-8 -*-

def city_saver(a, b):
    monster = 0
    for i in range(len(b)):
        monster += min(a[i], b[i])
        if(a[i]<b[i]):
            monster += min(b[i]-a[i], a[i+1])
            a[i+1] -= min(b[i]-a[i], a[i+1])
    return monster

def main():
    n = int(input())
    a = map(int, input().split())
    b = map(int, input().split())

if __name__ == "__main__":
    try:
    main()


Fixed code:
#! /usr/bin/env python3
# -*- coding:utf-8 -*-

def city_saver(a, b):
    monster = 0
    for i in range(len(b)):
        monster += min(a[i], b[i])
        if(a[i]<b[i]):
            monster += min(b[i]-a[i], a[i+1])
            a[i+1] -= min(b[i]-a[i], a[i+1])
    return monster

def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    print(city_saver(a, b))

if __name__ == "__main__":
    main()
Buggy code:
sin = input()
n = int(sin)
sin = input()
mons = list(map(int, sin.split()))
sin = input()
yuus = list(map(int, sin.split()))
yuus = [0] + yuus

tkill = 0
rest = 0
for mi, yi in zip(reversed(mons), reversed(yuus)):
    tkill += min(mi, yi + rest)
    rest = yi + rest - mi
    if rest < 0:
        rest = 0

print(tkill)

Fixed code:
sin = input()
n = int(sin)
sin = input()
mons = list(map(int, sin.split()))
sin = input()
yuus = list(map(int, sin.split()))
yuus = [0] + yuus

tkill = 0
rest = 0
for mi, yi in zip(reversed(mons), reversed(yuus)):
    tkill += min(mi, yi + rest)
    rest = yi + rest - mi
    if rest < 0:
        rest = 0
    elif rest > yi:
        rest = yi

print(tkill)
Buggy code:
N = int(input())
A = [int(x) for x in input().split(" ")]
B = [int(x) for x in input().split(" ")]

count = 0
for i in range(N):
    defeat = min([B[i], A[i]])
    B[i] -= defeat
    count += defeat
    defeat = min([B[i], A[i + 1]])
    A[i + 1] -= defeat
    B[i] -= defeat
    count += defeat

print(defeat)

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

count = 0
for i in range(N):
    n_defeat = min(A[i], B[i])
    A[i] -= n_defeat
    B[i] -= n_defeat
    count += n_defeat
    n_defeat = min(A[i + 1], B[i])
    A[i + 1] -= n_defeat
    B[i] -= n_defeat
    count += n_defeat

print(count)
Buggy code:
n =int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
sum = 0
for i in range(n):
  sum += min(A[i],B[i])
  if A[i] < B[i]:
    sum += B[i] - A[i]
    if B[i] >= A[i]+A[i+1]:
      A[i+1] = 0
    else:
      A[i+1] -= B[i]-A[i]
print(sum)
                    
                   
   

Fixed code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for i in range(n)[::-1]:
  if (A[i+1] + A[i]) <= B[i]:
    ans += (A[i+1]+A[i])
    A[i], A[i+1] = 0, 0
  else:
    ans += B[i]
    if A[i+1] - B[i] >= 0:
      A[i+1] = A[i+1] - B[i]
    else:
      A[i] = (A[i+1] + A[i] - B[i]) if (A[i+1] + A[i] - B[i]) else 0
      A[i+1] = 0

print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for i in range(N):
    ans += min(A[i] + A[i+1], B[i])
    A[i+1] -= max(B[i] - A[i], 0)
print(ans)


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

ans = 0
for i in range(N):
    if A[i] >= B[i]:
        A[i] -= B[i]
        ans += B[i]
        B[i] = 0 
    else:
        ans += A[i]
        B[i] -= A[i]
        ans += min(B[i], A[i+1])
        A[i+1] = max(0, A[i+1]-B[i])

print(ans)
Buggy code:
N = int(input())
monster = list(map(int, input().split()))
hero = list(map(int, input().split()))

total = 0
i = -1
print(i, total, monster, hero)

for i in range(N + 1):
    if i == 0:
        if monster[0] < hero[0]:
            total += monster[0]
            hero[0] -= monster[0]
        else:
            total += hero[0]
            hero[0] = 0
    elif i < N:
        if monster[i] < hero[i - 1]:
            total += monster[i]
        else:
            total += hero[i - 1]
            monster[i] -= hero[i - 1]
            
            if monster[i] < hero[i]:
                total += monster[i]
                hero[i] -= monster[i]
            else:
                total += hero[i]
                hero[i] = 0
    else:
        if monster[N] < hero[N - 1]:
            total += monster[N]
        else:
            total += hero[N - 1]
    print(i, total, monster, hero)
print(total)

Fixed code:
N = int(input())
monster = list(map(int, input().split()))
hero = list(map(int, input().split()))

total = 0
i = -1

for i in range(N + 1):
    if i == 0:
        if monster[0] < hero[0]:
            total += monster[0]
            hero[0] -= monster[0]
            monster[0] = 0
        else:
            total += hero[0]
            monster[0] -= hero[0]
            hero[0] = 0
            
    elif i < N:
        if monster[i] < hero[i - 1]:
            total += monster[i]
            hero[i - 1] -= monster[i]
            monster[i] = 0
        else:
            total += hero[i - 1]
            monster[i] -= hero[i - 1]
            hero[i - 1] = 0
            
            if monster[i] < hero[i]:
                total += monster[i]
                hero[i] -= monster[i]
                monster[i] = 0
            else:
                total += hero[i]
                hero[i] = 0
                monster[i] -= hero[i]
    else:
        if monster[N] < hero[N - 1]:
            total += monster[N]
        else:
            total += hero[N - 1]

print(total)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for i in range(N):
    ans += min(A[i] + A[i+1], B[i])
    A[i+1] -= min(B[i] - A[i], 0)
print(ans)


Fixed code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans, x = 0, 0
ans += x
for i in range(N):
    x = min(A[i]-x,B[i])
    ans += x
    x = min(A[i+1],B[i]-x)
    ans += x
print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0
for i in range(N):
    ans += min(A[i] + A[i+1], B[i])
    A[i+1] -= min(max(A[i] - B[i], 0), A[i+1])

print(ans)


Fixed code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
for i in range(N):
  tmp=min(A[i],B[i])
  ans+=tmp
  tmp=min(A[i+1],B[i]-tmp)
  A[i+1]-=tmp
  ans+=tmp
print(ans)
Buggy code:
n=int(input())
As=list(map(int,input().split()))
Bs=list(map(int,input().split()))
Bs.append(0)

ans=0
for i in range(n):
  c=As[i]+As[i+1]
  if c>Bs[i]:
    ans+=Bs[i]
    if Bs[i]>As[i]:
      As[i+1]-=Bs[i]
  else:
    ans+=c
    As[i+1]=0
print(ans)

Fixed code:
n=int(input())
As=list(map(int,input().split()))
Bs=list(map(int,input().split()))
Bs.append(0)

ans=0
for i in range(n):
  c=As[i]+As[i+1]
  if c>Bs[i]:
    ans+=Bs[i]
    if Bs[i]>As[i]:
      As[i+1]-=Bs[i]-As[i]
  else:
    ans+=c
    As[i+1]=0
print(ans)
Buggy code:
N = int(input()) 
B= [int(x) for x in input().split()]
A= [int(x) for x in input().split()]
all_ene = sum(B)
c = 0
for i in range(N):
  if B[i] >= A[i]:
    B[i] = B[i]-A[i]
  else:
    B[i] = 0
    B[i+1] = max(0,B[i+1]+B[i]-A[i])
    
print(all_ene-sum(B))

Fixed code:
N = int(input()) 
B= [int(x) for x in input().split()]
A= [int(x) for x in input().split()]
all_ene = sum(B)
c = 0
for i in range(N):
  if B[i] >= A[i]:
    B[i] = B[i]-A[i]
  else:
    B[i+1] = max(0,B[i+1]+B[i]-A[i])
    B[i] = 0

print(all_ene-sum(B))
Buggy code:
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
a1=a
for i in range(n):
  if b[i]>=a[i]+a[i+1]:
    a[i],a[i+1]=0,0
  elif b[i]>=a[i]:
    a[i+1]=b[i]-a[i]
    a[i]=0
  else:
    a[i]-=b[i]
print(sum(a1)-sum(a))
    

Fixed code:
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
asum=sum(a)
a1=a
ans=0
for i in range(n):
  if b[i]>=a1[i]+a1[i+1]:
    a1[i],a1[i+1]=0,0
    ans+=a1[i]+a1[i+1]
  elif b[i]>=a1[i]:
    a1[i+1]-=b[i]-a1[i]
    a1[i]=0
    ans+=b[i]
  else:
    a1[i]-=b[i]
    ans+=b[i]
print(asum-sum(a1))
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
KO = 0

for i in range(N):
  if  A[i] + A[i+1]> B[i]:
    KO += B[i]
    A[i+1] -= (B[i] - A[i])
  else:
    KO += A[i] + A[i+1]
    A[i+1] = 0
  
print(KO)


Fixed code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sum = 0
for i in range(N):
    if A[i] > B[i]:
        sum += B[i]
    else:
        sum += A[i]
        if A[i+1] > B[i]-A[i]:
            sum += B[i]-A[i]
            A[i+1] -= B[i]-A[i]
        else:
            sum += A[i+1]
            A[i+1] = 0
print(sum)
Buggy code:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

enemy = 0
for i in range(N):
    if A[i] >= B[i]:
        enemy += B[i]
        A[i] -= B[i]
    else:
        q = B[i]-A[i]
        A[i] = 0
        if q <= A[i+1]:
            enemy += B[i]
            A[i+1] -= q
        else:
            enemy += A[i]+A[i+1]
            A[i+1] = 0

print(enemy)


Fixed code:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

enemy = 0
for i in range(N):
    if A[i] >= B[i]:
        enemy += B[i]
    else:
        q = B[i]-A[i]
        if q <= A[i+1]:
            enemy += B[i]
            A[i+1] -= q
        else:
            enemy += A[i]+A[i+1]
            A[i+1] = 0

print(enemy)
Buggy code:
N=int(input())
B=list(map(int,input().split()))
A=list(map(int,input()))
for i in range(N):
  if B[i]>=A[i]:
    B[i]-=A[i]
  else:
    B[i]-=A[i]
    B[i+1]=B[i+1]-B[i]+A[i]
print(sum(B))


Fixed code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
sumA = sum(A)

for i in range(N):
    if A[i] >= B[i]:
        A[i] = A[i] - B[i]
    else:
        c = B[i]-A[i]
        A[i] = 0
        if A[i+1]>=c:
            A[i+1] = A[i+1]-c
        else:
            A[i+1] =0

new_sumA = sum(A)

print(sumA - new_sumA)
Buggy code:
N = int( input() )
A_list = list( map( int, input().split() ) )
B_list = list( map( int, input().split() ) )
c = max(A_list[0], B_list[0])
remain_B = max(B_list[0] - A_list[0], 0)
for i in range(1, N ):
    c += min(remain_B, A_list[i])
    remain_A = max( A_list[i] - remain_B, 0 )
    c += min(remain_A, B_list[i])
    remain_B = max(B_list[i] - remain_A, 0 )
c += max(A_list[N], remain_B)
print(c)

Fixed code:
N = int( input() )
A_list = list( map( int, input().split() ) )
B_list = list( map( int, input().split() ) )
c = min(A_list[0], B_list[0])
remain_B = max(B_list[0] - A_list[0], 0)
for i in range(1, N ):
    c += min(remain_B, A_list[i])
    remain_A = max( A_list[i] - remain_B, 0 )
    c += min(remain_A, B_list[i])
    remain_B = max(B_list[i] - remain_A, 0 )
c += min(A_list[N], remain_B)
print(c)
Buggy code:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
L = 0
for i in range(len(B)):
    i_ = N-i-1
    if A[i_ +1]<= B[i_]:
        B_ = B[i_]-A[i_+1]
        A[i_+1]=0
        A[i_]=A[i_]-B_
        if A[i_]<0:
            L += A[i_]
        L += B[i_]
    else:
        L += B[i_]
        A[i_+1]-=B[i_]
print(L)

Fixed code:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
L = 0
for i in range(len(B)):
    i_ = N-i-1
    if A[i_ +1]<= B[i_]:
        B_ = B[i_]-A[i_+1]
        A[i_+1]=0
        A[i_]=A[i_]-B_
        if A[i_]<0:
            L += A[i_]
            A[i_]=0
        L += B[i_]
    else:
        L += B[i_]
        A[i_+1]-=B[i_]
print(L)
Buggy code:
#!/usr/bin/env python
# coding: utf-8

# In[26]:


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


# In[27]:


ans = 0
for i in range(N):
    ans += min(A[i], B[i])
    B[i] = max(B[i]-A[i])


# In[ ]:






Fixed code:
#!/usr/bin/env python
# coding: utf-8

# In[26]:


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


# In[28]:


ans = 0
for i in range(N):
    ans += min(A[i], B[i])
    B[i] = max(B[i]-A[i], 0)
    ans += min(A[i+1], B[i])
    A[i+1] = max(A[i+1]-B[i], 0)
print(ans)


# In[ ]:
Buggy code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

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

print(ans)

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

ans=0
for i in range(n):
  ans+=min(a[i],b[i])
  b[i]-=min(a[i],b[i])
  ans+=min(a[i+1],b[i])
  a[i+1]-=min(a[i+1],b[i])
print(ans)
Buggy code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

monster_count = 0
next_saver = 0

for i in range(N+1):
    if A[i] > B[i] + next_saver:
        print(i)
        monster_count += B[i] + next_saver
        next_saver = 0
    else:
        monster_count += A[i]
        if A[i] <= next_saver:
            next_saver = B[i]
        else:
            next_saver = B[i] - (A[i]-next_saver)
print(monster_count)

Fixed code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

monster_count = 0
next_saver = 0

for i in range(N+1):
    if A[i] > B[i] + next_saver:
        monster_count += B[i] + next_saver
        next_saver = 0
    else:
        monster_count += A[i]
        if A[i] <= next_saver:
            next_saver = B[i]
        else:
            next_saver = B[i] - (A[i]-next_saver)
print(monster_count)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

sum = 0
for i in range(0,N):#0,1,,,,N-1
    print(i)
    if A[i] >= B[i]:
        sum += B[i]
    else:
        sum +=A[i]
        if A[i+1] >= B[i] -A[i]:
            A[i+1] -= B[i]-A[i]
            sum += B[i]-A[i]
        else:
            sum += A[i+1]
            A[i+1] = 0
print(sum)

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

sum = 0
for i in range(0,N):#0,1,,,,N-1
    if A[i] >= B[i]:
        sum += B[i]
    else:
        sum +=A[i]
        if A[i+1] >= B[i] -A[i]:
            A[i+1] -= B[i]-A[i]
            sum += B[i]-A[i]
        else:
            sum += A[i+1]
            A[i+1] = 0
print(sum)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = 0
for i in range(n):
  if a[i]>=b[i]:
    c+=int(a[i]-b[i])
  else:
    if (a[i]+a[i+1])>=b[i]:
      c+=int(b[i])
      a[i+1]=a[i]+a[i+1]-b[i]
    else:
      c+=int(b[i])
print(c)

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

count = 0
for i in range(n):
    if b[i] > a[i]:
        b[i] -= a[i]
        count += a[i]
    else:
        count += b[i]
        b[i] = 0
    if a[i+1] > b[i]:
        count += b[i]
        a[i+1] -= b[i]
    else:
        count += a[i+1]
        a[i+1] = 0

print(count)
Buggy code:
# coding: utf-8


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

    for i in range(N):
        b = int(input())
        tmp = A[i] - b
        if tmp < 0:
            ans += A[i]
            if A[i + 1] < -tmp:
                ans += A[i + 1]
                A[i + 1] = 0
            else:
                ans += -tmp
                A[i + 1] += tmp
        else:
            ans += b

    print(ans)

if __name__ == "__main__":
    main()


Fixed code:
# coding: utf-8


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = 0

    for i in range(N):
        tmp = A[i] - B[i]
        if tmp < 0:
            ans += A[i]
            if A[i + 1] < -tmp:
                ans += A[i + 1]
                A[i + 1] = 0
            else:
                ans += -tmp
                A[i + 1] += tmp
        else:
            ans += B[i]

    print(ans)

if __name__ == "__main__":
    main()
Buggy code:
#coding: UTF-8

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

Ans = 0

for num in range(N):
    Ans += min(A[num], B[num])
    if A[num] => B[num]:
        pass
    else: # A[num] < B[num]
        B[num] -= A[num]
        Ans += min(A[num+1], B[num])
        if A[num+1] => B[num]:
            A[num+1] -= B[num]
        else:
            A[num+1] = 0
print(Ans)

Fixed code:
#coding: UTF-8

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

Ans = 0

for num in range(N):
    Ans += min(A[num], B[num])
    if A[num] >= B[num]:
        pass
    else: # A[num] < B[num]
        B[num] -= A[num]
        Ans += min(A[num+1], B[num])
        if A[num+1] >= B[num]:
            A[num+1] -= B[num]
        else:
            A[num+1] = 0
print(Ans)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
for i in range(n):
    if a[i] < b[i]:
       
        s = b[i] - a[i] if a[i+1] - (b[i] - a[i]) >= 0 else a[i+1]
        ans += a[i] + s

        a[i+1] = max(a[i+1] - (b[i] - a[i]),0)
        a[i] = 0

        #print(a[i] + s)

    else:
        a[i] -= b[i]
        ans += b[i]
        print(b[i])
#print(a)
print(ans)

Fixed code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
for i in range(n):
    if a[i] < b[i]:
       
        s = b[i] - a[i] if a[i+1] - (b[i] - a[i]) >= 0 else a[i+1]
        ans += a[i] + s

        a[i+1] = max(a[i+1] - (b[i] - a[i]),0)
        a[i] = 0

        #print(a[i] + s)

    else:
        a[i] -= b[i]
        ans += b[i]
        #print(b[i])
#print(a)
print(ans)