qyliang/CodeNanoFix huggingface dataset:
Buggy code:
def main():
  import sys
  n = int(input())
  a = list(map(int.sys.stdin.readline().split()))
  b = list(map(int.sys.stdin.readline().split()))
  
  count=0
  
  for i in range(n):
    c1 = a[i] 
    c2 = a[i+1]
    h = b[i]
    ans+=min(c1,h)  
    if h>c1:
        ans+=min(h-c1,c2)
        a[i+1]=max(0,c2-(h-c1))

  print(ans)
  
if __name__=="__main__":
  main()

Fixed code:
def main():
  import sys
  n = int(input())
  a = list(map(int,sys.stdin.readline().split()))
  b = list(map(int,sys.stdin.readline().split()))
  
  count=0
  
  for i in range(n):
    c1 = a[i] 
    c2 = a[i+1]
    h = b[i]
    count+=min(c1,h)  
    if h>c1:
        count+=min(h-c1,c2)
        a[i+1]=max(0,c2-(h-c1))
 
  print(count)
  
if __name__=="__main__":
  main()
Buggy code:
N = int(input())
A = [int(x) for x in input.split()]
B = [int(x) for x in input.split()]

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

print(tot)


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

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

print(tot)
Buggy code:
n = int(input())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
amari = a_list[0]
kill = 0
for i in range(n):
  b = b_list[i]
  if amari > b:
    b = 0
    kill += b
  else:
    b -= amari
    kill += amari
  if b > a_list[i+1]:
    kill += a_list[i+1]
    amari = 0
  else:
    kill += b
    amari = a_list[i+1] - b
print(kill)

Fixed code:
n = int(input())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
amari = a_list[0]
kill = 0
for i in range(n):
  b = b_list[i]
  if amari > b:
    kill += b
    b = 0
  else:
    b -= amari
    kill += amari
  if b > a_list[i+1]:
    kill += a_list[i+1]
    amari = 0
  else:
    kill += b
    amari = a_list[i+1] - b
print(kill)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.reverse()
B.reverse()
ans = 0
t = A[0]
for a, b in zip(A[1:], B):
    t += a
    print(ans, t)
    m = min(t, b)
    ans += m
    t = t - m if t - a <= b else a

print(ans)


Fixed code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.reverse()
B.reverse()
ans = 0
t = A[0]
for a, b in zip(A[1:], B):
    t += a
    m = min(t, b)
    ans += m
    t = t - m if t - a <= b else a

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

    if judge < 0:
        A[i+1] += judge
        if A[i+1] < 0:
            A[i+1] = 0
print(count)

Fixed code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
count = 0
for i in range(N):
    judge = A[i] - B[i]
    if judge <= 0:
        count += A[i]
    else:
        count += B[i]

    if judge < 0:
        if (A[i+1]+judge) >= 0:
            count += -judge
            A[i+1] += judge
        else:
            count += A[i+1]
            A[i+1] = 0
print(count)
Buggy code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0

for i in range(n):
    print(a,b)
    if b[i] >= sum(a[i:i+2]):
        ans += sum(a[i:i+2])
        a[i+1] = 0
    elif a[i] >= b[i]:
        ans += b[i]
    elif a[i] < b[i]:
        ans += b[i]
        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):
    if b[i] >= sum(a[i:i+2]):
        ans += sum(a[i:i+2])
        a[i+1] = 0
    elif a[i] >= b[i]:
        ans += b[i]
    elif a[i] < b[i]:
        ans += b[i]
        a[i+1] -= b[i] - a[i]
print(ans)
Buggy code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
for i in range(n):
  a[i]=max(a[i]-b[i],0)
  b[i]=max(b[i]-a[i],0)
for i in range(n):
  a[i+1]=max(b[i]-a[i+1],0)
print(sum(a))

Fixed code:
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
b=list(map(int,input().split()))
for i in range(n):
  x=max(a[i]-b[i],0)
  b[i]=max(b[i]-a[i],0)
  a[i]=x
  a[i+1]=max(a[i+1]-b[i],0)
print(s-sum(a))
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]:ans+=b[i]
  else:
    ans+=a[i]
    if a[i+1]+a[i]<b[i]:
      a[i+1]=0
      ans+=a[i+1]
    else:
      ans+=b[i]-a[i+1]
      a[i+1]=a[i+1]+a[i]-b[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):
    if b[i] <= a[i]:
        ans += b[i]
    else:
        ans += a[i]
        b[i] -= a[i]
        if b[i] <= a[i+1]:
            a[i+1] -= b[i]
            ans += b[i]
        else:
            ans += a[i+1]
            a[i+1] = 0

print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cn = 0

for i in range(N):
    
    if B[i] <= A[i]:
        
        cn = cn + B[i]
    elif B[i] > A[i] and B[i] < (A[i]+A[i+1]) :
        cn = cn + B[i] + (A[i]-B[i])
        A[i+1] = A[i+1] - (B[i] - A[i])
    elif B[i] >= A[i] + A[i+1]:
        cn = cn + A[i] + A[i+1]
        A[i] = 0
        A[i+1] = 0
        
      
print(cn)

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

for i in range(N):
    
    if B[i] <= A[i]:
        
        cn = cn + B[i]
    elif B[i] > A[i] and B[i] < (A[i]+A[i+1]) :
        cn = cn + B[i]
        A[i+1] = A[i+1] - (B[i] - A[i])
    elif B[i] >= A[i] + A[i+1]:
        cn = cn + A[i] + A[i+1]
        A[i] = 0
        A[i+1] = 0
        
      
print(cn)
Buggy code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))

    ans = 0
    for i in range(N):
        attack = min(A[i],B[i])
        A[i] -= attack
        B[i] -= attack
        ans += attack
        print('i: {}, attack: {}'.format(i,attack))
        if B[i] == 0:
            continue
        else:
            attack = min(A[i+1],B[i])
            A[i+1] -= attack
            B[i] -= attack
            ans += attack
            print('i: {}, attack: {}'.format(i,attack))
    print(ans)


if __name__ == '__main__':
    main()


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

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


if __name__ == '__main__':
    main()
Buggy code:
def resolve():

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

    ans = 0

    for i in range(n):

        beat_now = min(a[i], b[i])
        ans += beat_now
        beat_next = min(a[i+1], b[i] - min(a[i], b[i]))
        ans += beat_next
        a[i+1] -= beat_next

    print(ans)

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

    ans = 0

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

    print(ans)


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

ans = 0

for i in range(n):
  sa = a[i] - b[i]
  
  if sa < 0:
    b[i] -= a[i]
    ans += a[i]
    sa2 = a[i+1] - b[i]
    
    if sa2 < 0:
      ans += a[i+1]
      a[i+1] = 0
    else:
      a[i+1] -= sa2
      ans += b[i]
      b[i] -= sa2
      
  else:
    ans += b[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):
    if a[i]>b[i]:
        ans+=b[i]
    else:
        ans+=a[i]
        b[i]-=a[i]
        if a[i+1]<=b[i]:
            ans+=a[i+1]
            a[i+1]=0
        else:
            ans+=b[i]
            a[i+1]-=b[i]
print(ans)
Buggy code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
p=sum(a)
b.append(0)
for i in range(len(a)):
    if a[i]-b[i]<0:
        if a[i]-b[i]<0:
            a[i+1]=0
        else:
            a[i+1]-=b[i]-a[i]
        a[i]=0
    else:
        a[i]-=b[i]
print(p-sum(a))

Fixed code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
p=sum(a)
b.append(0)
for i in range(len(a)):
    if a[i]-b[i]<0:
        if a[i+1]-b[i]+a[i]<0:
            a[i+1]=0
        else:
            a[i+1]-=b[i]-a[i]
        a[i]=0
    else:
        a[i]-=b[i]
print(p-sum(a))
Buggy code:
b=[int(i) for i in input().split()]
sum=np.sum(b)
for i in reversed(range(n)):
  k = a[i+1]
  l = a[i]
  m = b[i]
 
  a[i+1] = max(k-m, 0)
  b[i] = max(m-k, 0)
 
  a[i] = max(l-b[i], 0)
  b[i] = max(b[i]-l, 0)
postsum=np.sum(b)
print(sum-postsum)
 

Fixed code:
import numpy as np
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
sum=np.sum(b)
for i in reversed(range(n)):
  k = a[i+1]
  l = a[i]
  m = b[i]
 
  a[i+1] = max(k-m, 0)
  b[i] = max(m-k, 0)
 
  a[i] = max(l-b[i], 0)
  b[i] = max(b[i]-l, 0)
postsum=np.sum(b)
print(sum-postsum)
Buggy code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
num=0
for i in range(n):
  num+=min(a[i],b[i])
  b[i]=max(0,b[i]-a[i])
  num+=min(b[i]-a[i],a[i+1])
  a[i+1]=max(0,a[i+1]-b[i])
print(num)

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

sum_monster = 0
add = 0

def trp(l, n):
    return l[:n] + [0]*(n-len(l))
B = trp(B, len(A))

for (mons, hero) in zip(A, B):
  hero_added = hero + add
  if mons >= hero_added:
    sum_monster += hero_added
    add = 0
  else:
    sum_monster += mons
    if hero_added >= hero:
      add = hero
    else:
      add = (hero_added - mons)
print(sum_monster)



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

sum_monster = 0
add = 0

def trp(l, n):
    return l[:n] + [0]*(n-len(l))
B = trp(B, len(A))

for (mons, hero) in zip(A, B):
  hero_added = hero + add
  if mons >= hero_added:
    sum_monster += hero_added
    add = 0
  else:
    sum_monster += mons
    if (hero_added - mons) >= hero:
      add = hero
    else:
      add = (hero_added - mons)
print(sum_monster)
Buggy code:
a = int(input())
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = 0
for i in range(a-1, -1, -1):
    d += min(b[i+1], c[i])
    b[i+1] -= min(b[i+1], c[i])
    c[i] -= min(b[i+1], c[i])

    d += min(b[i], c[i])
    b[i] -= min(b[i], c[i])
    c[i] -= min(b[i], c[i])
print(d)


Fixed code:
a = int(input())
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = 0
for i in range(a-1, -1, -1):
    e = min(b[i+1], c[i])
    d += e
    b[i+1] -= e
    c[i] -= e
    e = min(b[i], c[i])
    d += e
    b[i] -= e
    c[i] -= e
print(d)
Buggy code:
N,A,B=open(0).read().rstrip().split('\n')
N=int(N)
A=list(map(int,A.split()))
B=list(map(int,B.split()))
A.sort(reverse=True)
B.sort(reverse=True)
hunt=0
for i in range(N):
    if A[i]>=B[i]:
        hunt+=B[i]
    elif A[i]+A[i+1]>=B[i]:
        hunt+=B[i]
        suplus=B[i]-A[i]
        A[i+1]=A[i+1]-suplus
    else:
        hunt+=A[i]+A[i+1]
        A[i+1]=0
print(hunt)

Fixed code:
N,A,B=open(0).read().rstrip().split('\n')
N=int(N)
A=list(map(int,A.split()))
B=list(map(int,B.split()))
hunt=0
for i in range(N):
    if A[i]>=B[i]:
        hunt+=B[i]
    elif A[i]+A[i+1]>=B[i]:
        hunt+=B[i]
        suplus=B[i]-A[i]
        A[i+1]=A[i+1]-suplus
    else:
        hunt+=A[i]+A[i+1]
        A[i+1]=0
print(hunt)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans, mod = 0, 0

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

    if B[i] > A[i]:
        ans += A[i]
        mod = B[i] - A[i]
    else:
        ans += B[i]
        mod = 0

print(ans)


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

ans, mod = 0, 0

for i in range(N):

    if mod > A[i]:
        ans += A[i]
        A[i] = 0
    else:
        ans += mod
        A[i] -= mod
        mod = 0

    if B[i] > A[i]:
        ans += A[i]
        mod = B[i] - A[i]
    else:
        ans += B[i]
        mod = 0

ans += min(mod, A[N])

print(ans)
Buggy code:
import numpy as np
N = int(input())
A = [int(i) for i in input().split()] 
B = [int(i) for i in input().split()] 
C = np.zeros(N+1)
count = 0
for i in range(N):
  a = A[i] - B[i] -C[i]
  if a >= 0:
    count += B[i]
  else:
    count += A[i]
    C[i+1] = -a
if C[N] > 0:
  if A[N] < C[N]:
    count += A[N]
  else:
    count += C[N]
print(count)

Fixed code:
import numpy as np
N = int(input())
A = [int(i) for i in input().split()] 
B = [int(i) for i in input().split()] 
C = np.zeros(N+1)
count = 0
for i in range(N):
  a = A[i] - B[i] -C[i]
  if a >= 0:
    count += B[i] + C[i]
  else:
    count += A[i]
    if A[i] - C[i] < 0:
      C[i+1] = B[i]
    else:
      C[i+1] = -a
if C[N] > 0:
  if A[N] < C[N]:
    count += A[N]
  else:
    count += C[N]
print(int(count))
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans, mod = 0, 0

for i in range(N):

    if mod > A[i]:
        ans += A[i]
        A[i] = 0
    else:
        ans += mod
        A[i] -= mod
        mod = 0

    if B[i] > A[i]:
        ans += A[i]
        mod = B[i] - A[i]
    else:
        ans += B[i]
        mod = 0

ans += min(mod, A[i])

print(ans)


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

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

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]:
        count += b[i]
    else:
        p = b[i]-a[i]
        count += a[i]
        if p > a[i+1]:
            count += a[i+1]
            a[i+1] = 0
        else:
            count += p
            a[i+1] -= p
print(count)
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] + a[i + 1] < b[i]:
        ans += a[i] + a[i + 1]
        a[i + 1] = 0
    else:
        ans += b[i]
        if a[i] < b[i]:
            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):
    if a[i] + a[i + 1] < b[i]:
        ans += a[i] + a[i + 1]
        a[i + 1] = 0
    else:
        ans += b[i]
        if a[i] < b[i]:
            a[i + 1] -= b[i] - a[i]

print(ans)
Buggy code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

#dp[i] = dp[i-1]

target = a[0]
ans = 0

for i in range(n):
#    print("\n# i: " + str(i))

    if b[i] > target:
#        print("# Terminate all!")
        ans += target
        res = b[i] - target
        ans += min(res, a[i+1])
        target = res - a[i+1]
        if target < 0:
            target = 0

#        print("# ans: " + str(ans))
    else:
#        print("# Do my best...")
        ans += b[i]
        target = a[i+1]
#        print("# ans: " + str(ans))

print(ans)

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

target = a[0]
ans = 0

for i in range(n):
#    print("\n# i: " + str(i))

    if b[i] > target:
#        print("# Terminate all!")
        ans += target
        res = b[i] - target
        ans += min(res, a[i+1])
        target = a[i+1] - res
        if target < 0:
            target = 0

        # print("# ans: " + str(ans))
        # print("# res: " + str(res))
        # print("# target: " + str(target))
    else:
#        print("# Do my best...")
        ans += b[i]
        target = a[i+1]
#        print("# ans: " + str(ans))

print(ans)
Buggy code:
N = int(input())


S = input()
S = S.split()
A = list(map(int, S))
A_copy = list(map(int, S))

S = input()
S = S.split()
B = list(map(int, S))


remain = 0

for i in range(len(B)):
    remain = int(A[i]-B[i])
    if remain <= 0:
        if A[i+1]+remain >= 0:
            A[i+1] = int(A[i+1] + remain)
        else:
            A[i+1] = 0
        A[i] = 0
    elif remain > 0:
        A[i] = remain



if A[N] == 0:
    print(sum(A_copy)-sum(A))


Fixed code:
N = int(input())


S = input()
S = S.split()
A = list(map(int, S))
A_copy = list(map(int, S))

S = input()
S = S.split()
B = list(map(int, S))


remain = 0

for i in range(len(B)):
    remain = int(A[i]-B[i])
    if remain <= 0:
        if A[i+1]+remain >= 0:
            A[i+1] = int(A[i+1] + remain)
        else:
            A[i+1] = 0
        A[i] = 0
    elif remain > 0:
        A[i] = remain



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

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

sum_ = 0
rest = 0

for i in range(n):
  if a[i] >= b[i] + rest:
    sum_ += b[i] + rest
    rest = 0
  elif a[i] >= rest:
    sum_ += a[i]
    rest = b[i]
  else:
    sum_ += a[i]
    rest = b[i]

sum_ += min(a[n], rest)
print(sum_)

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

sum_ = 0
rest = 0

for i in range(n):
  if a[i] >= b[i] + rest:
    sum_ += b[i] + rest
    rest = 0
  elif a[i] >= rest:
    sum_ += a[i]
    rest = b[i] - (a[i]- rest)
  else:
    sum_ += a[i]
    rest = b[i]

sum_ += min(a[n], rest)
print(sum_)
Buggy code:
N=int(input())
M=N+1
A=[int(M) for M in input().split()]
B=[int(N) for N in input().split()]
num=0
for i in range(N,1,-1):
     if B[i-1]>=A[i]:
          B[i-2]+=B[i-1]-A[i]
          num+=A[i]
     else:
          num+=B[i-1]
if B[0]>A[0]+A[1]:
     num+=A[0]+A[1]
else:
     num+=B[0]
print(num)


Fixed code:
N=int(input())
M=N+1
A=[int(M) for M in input().split()]
B=[int(N) for N in input().split()]
num=0
for i in range(N,1,-1):
     if B[i-1]>=A[i]:
          num+=A[i]
          a=A[i-1]
          A[i-1]-=B[i-1]-A[i]
          if A[i-1]<0:
               A[i-1]=0
               num+=a
          else:
               num+=B[i-1]-A[i]
     else:
          num+=B[i-1]
if B[0]>A[0]+A[1]:
     num+=A[0]+A[1]
else:
     num+=B[0]
print(num)
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 b[i] <= a[i]:
        ans += b[i]
    elif b[i] < a[i] + a[i+1]:
        a[i+1] = a[i] - b[i]
        ans += b[i]
    else:
        ans += a[i] + a[i+1]
        a[i+1] = 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(b[i] > a[i]):
        ans += a[i]
        tmp = b[i] - a[i]
        if(tmp < a[i+1]):
            ans += tmp
            a[i+1] = a[i+1] - tmp
        else:
            ans += a[i+1]
            a[i+1] = 0
    else:
        ans += b[i]

print(ans)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
count = 0
print(a)
print(b)
for i in range(n):
    if b[i] >= a[i] + a[i+1]:
        count += a[i] + a[i+1]
        a[i+1] = 0
    elif b[i] > a[i] and b[i] < a[i] + a[i+1]:
        count += b[i]
        a[i+1] -= b[i] - a[i]
    elif a[i] >= b[i]:
        count += b[i]
print(count)
            


Fixed code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
for i in range(n):
    if a[i] < b[i]:
        b[i] = b[i] - a[i] 
        cnt += a[i]
        a[i] = 0
        
        if a[i+1] < b[i]:
            b[i] = b[i] - a[i+1] 
            cnt += a[i+1]
            a[i+1] = 0
        
        elif a[i+1] >= b[i]:
            a[i+1] = a[i+1] - b[i]
            cnt += b[i]
    elif a[i] >= b[i]:
        a[i] = a[i] - b[i]
        cnt += b[i]
print(cnt)
Buggy code:
N = int(input())
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
rest = 0
for i in range(N):
  if M[i] >= H[i]+rest:
    rest = 0
    sum_m += H[i]+rest
  else:
    rest += H[i]-M[i]
    sum_m += M[i]
sum_m += min(rest, M[-1])
rest = 0
sum_n = 0
for i in range(N)[::-1]:
  if M[i+1] >= H[i]+rest:
    rest = 0
    sum_n += H[i]+rest
  else:
    rest += H[i]-M[i+1]
    sum_n += M[i+1]
sum_n += min(rest, M[0])
print(max(sum_m, sum_n))


Fixed code:
N = int(input())
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
rest = 0
for i in range(N):
  if M[i] >= H[i]+rest:
    sum_m += H[i]+rest
    rest = 0
  else:
    sum_m += M[i]
    rest = min(H[i], H[i]+rest-M[i])
sum_m += min(rest, M[-1])

print(sum_m)
Buggy code:
N = int(input())
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
rest = 0
for i in range(0, N):
  killed = min(H[i], M[i]-rest)
  rest = max(0, H[i]+rest-M[i])
  sum_m = killed
sum_m += min(rest, M[-1])

print(sum_m)


Fixed code:
N = int(input())
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
killed = 0
rest = 0
for i in range(0, N):
  killed = min(H[i]+rest, M[i])
  rest = max(0, min(H[i]+rest-killed, H[i]))
  sum_m += killed
sum_m += min(rest, M[-1])
print(sum_m)