qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n=int(input())
*a,=map(int,input().split())
*b,=map(int,input().split())
cnt=0
for i in range(n):
    if a[i]>=b[i]:
        cnt += b[i]
    else:
        cnt += a[i]
        rest=b[i]-a[i]
        if rest <= a[i+1]:
            a[i+1] -= rest
            cnt += rest
        else:
            a[i+1]=0
            cnt += a[i+1]
print(cnt)

Fixed code:
n=int(input())
*a,=map(int,input().split())
*b,=map(int,input().split())
cnt=0
for i in range(n):
    if a[i]>=b[i]:
        cnt += b[i]
        a[i] -= b[i]
    else:
        cnt += a[i]
        b[i]-=a[i]
        a[i]=0
        if b[i] <= a[i+1]:
            a[i+1] -= b[i]
            cnt += b[i]
        else:
            cnt += a[i+1]
            a[i+1]=0
print(cnt)
Buggy 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]<=b[i]:
        x+=a[i]
        if a[i+1]>=b[i]:
            a[i+1]-=b[i]-a[i]
            x+=b[i]-a[i]
        else:
            x+=a[i+1]
            a[i+1]=0
            
        a[i]=0
    else:
        x+=b[i]
        a[i]-=b[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 b[i]>=a[i]:
        x+=a[i]
        b[i]-=a[i]
        a[i]=0
        if a[i+1]>=b[i]-a[i]:
            x+=b[i]-a[i]
            a[i+1]-=b[i]-a[i]
            b[i]=0
        else:
            x+=a[i+1]
            a[i+1]=0
            b[i]-=a[i+1]
    else:
        x+=b[i]
        a[i]-=b[i]
        b[i]=0 
print(x)
Buggy code:
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *

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]
        continue
    else:
        ans += min(a[i]+a[i+1], b[i])
        a[i+1] = max(0, a[i+1]-b[i]-a[i])
print(ans)


Fixed code:
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *

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]
        continue
    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:
num_lines = int(input())
if num_lines<1 or num_lines>10**5:
	print ('Illegal')
	exit()
#print(num_lines)
a=list(map(int,input().split(" ")))
b=list(map(int,input().split(" ")))

if len(a) !=num_lines +1 or len(b)!=num_lines:
	print ('Illegal')
	exit()

#print (a)

s=0
r=0
for i in range(num_lines):
	x=min(a[i]-r,b[i])
	s+=x
	r=0
	if(x<b[i]):
		y=min(a[i+1],b[i]-x)
		s+=y
		r=a[i+1]-y
print(s)


Fixed code:
num_lines = int(input())
if num_lines<1 or num_lines>10**5:
	print ('Illegal')
	exit()
#print(num_lines)
a=list(map(int,input().split(" ")))
b=list(map(int,input().split(" ")))

if len(a) !=num_lines +1 or len(b)!=num_lines:
	print ('Illegal')
	exit()

#print (a)

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

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]:
        cnt+= a[i]
        b[i] -= a[i]
        if a[i+1] <= b[i]:
            cnt += a[i+1]
            a[i+1] = 0
        else:
            cnt += b[i]
            a[i+1] -= b[i]
    else:
        cnt += b[i]

print(cnt)
Buggy code:
# coding: utf-8
a = int(input())
mon = list(map(int, input().split(" ")))
att = list(map(int, input().split(" ")))
kil = 0

b = len(mon)

for i in range(b):
#    print(mon[i])
    if i == a :
        if  mon[i] >= att[i-1]:
            kil = kil + att[i-1]
        elif mon[i] < att[i-1] :
            kil = kil + mon[i]
    elif i > 0 and att[i-1] > 0 :
        if mon[i] >= att[i-1]:
            kil = kil + att[i-1]
            mon[i] = mon[i] - att[i-1] 
        elif mon[i] < att[i-1] :
            kil = kil + mon[i]
            continue
    elif mon[i] >= att[i]:
        kil = kil + att[i]
        att[i] = 0
    elif mon[i] < att[i] :
        kil = kil + mon[i]
        att[i] = att[i] - mon[i]
#    print('result'+str(i)+':')
#    print('kil:'+str(kil))
#    print('zanAtt:'+att[])
        
print(kil)

Fixed code:
# coding: utf-8
a = int(input())
mon = list(map(int, input().split(" ")))
att = list(map(int, input().split(" ")))
kil = 0

b = len(mon)

for i in range(b):
#    print(mon[i])
    if i == a :
        if  mon[i] >= att[i-1]:
            kil = kil + att[i-1]
        elif mon[i] < att[i-1] :
            kil = kil + mon[i]
        break
    elif i > 0 and att[i-1] > 0 :
        if mon[i] >= att[i-1]:
            kil = kil + att[i-1]
            mon[i] = mon[i] - att[i-1] 
        elif mon[i] < att[i-1] :
            kil = kil + mon[i]
            continue
    if mon[i] >= att[i]:
        kil = kil + att[i]
        att[i] = 0
    elif mon[i] < att[i] :
        kil = kil + mon[i]
        att[i] = att[i] - mon[i]
#    print('result'+str(i)+':')
#    print('kil:'+str(kil))
#    print('zanAtt:'+att[])
        
print(kil)
Buggy code:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

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

print(cnt)

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

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

print(cnt)
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+=a[i]
        ans+=min(a[i+1],b[i]-a[i])
        a[i+1]=max(a[i+1]-(b[i]-a[i]),0)
    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+=a[i]
        ans+=min(a[i+1],b[i]-a[i])
        a[i+1]=max(a[i+1]-(b[i]-a[i]),0)
    else:
        ans+=b[i]
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):
    print(i)
    if A[i] > B[i]:
        ans += B[i]
        A[i] -= B[i]
        B[i] -= B[i]
    else:
        ans += A[i]
        B[i] -= A[i]
        A[i] -= A[i]
    if A[i+1] > B[i]:
        ans += B[i]
        A[i+1] -= B[i]
        B[i] -= B[i]
    else:
        ans += A[i+1]
        B[i] -= A[i+1]
        A[i+1] -= 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):
    if A[i]>=B[i]:
        ans+=B[i]
    else:
        a=A[i+1]
        A[i+1]=max(0,A[i+1]+A[i]-B[i])
        ans+=A[i]+(a-A[i+1])
print(ans)
Buggy code:
import copy

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

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

print(ans)
    


Fixed code:
import copy

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

ans = 0
for i in range(0,n):
    if A[i] <= B[i]:
        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]
    else:
        ans += B[i]

print(ans)
Buggy code:
#N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
fenum = sum(A)

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

print(fenum - sum(A))


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

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

print(fenum - sum(A))
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

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

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

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

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

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): 
    d = min(A[i], B[i]) 
    ans += d 
    A[i] -= d 
    B[i] -= d 
    d = min(A[i + 1], B[i]) 
    ans += d 
    A[i + 1] -= d 
    B[i] -= d 

print(ans)
Buggy code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
res = 0
for i in range(n):
    t1 = min(a[i], b[i])
    a[i] -= t1
    b[i] -= t1
    res += t
    t2 = min(a[i+1], b[i])
    a[i+1] -= t2
    b[i] -= t2
    res += t2
print(res)


Fixed code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
res = 0
for i in range(n):
    t1 = min(a[i], b[i])
    a[i] -= t1
    b[i] -= t1
    res += t1
    t2 = min(a[i+1], b[i])
    a[i+1] -= t2
    b[i] -= t2
    res += t2
print(res)
Buggy code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
cnt=0
for i in range(N):
  rem=B[i]-A[i]
  if rem<=0:
    cnt+=B[i]
  else:
    cnt+=A[i]
    A[i+1]-=rem
  if A[i+1]<0:
    A[i+1]=0
cnt+=rem

print(cnt)

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

print(num)

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

        else:
            num += zan
            A[i + 1] -= zan

    else:
        num += B[i]


#num += min(A[N], zan)

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


Fixed code:
n = int(input())
AL = list(map(int,input().split()))
BL = list(map(int,input().split()))
ans = 0
for i in range(n):
    if BL[i] <= AL[i]:
        ans += BL[i]
    elif BL[i] > AL[i] and BL[i] <= AL[i]+AL[i+1]:
        ans += BL[i]
        AL[i+1] = AL[i+1] - BL[i] + AL[i]
    else:
        ans += AL[i]+AL[i+1]
        AL[i+1] = 0
print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
Y = list(map(int, input().split()))
A_sum_1 = sum(A)
for i in range(N):
  if A[i] < Y[i]:
    Y[i] = Y[i] - A[i]
    A[i] = 0

  else:
    A[i] = A[i] - Y[i]
    Y[i] = 0

  if Y[i] > 0:
    A[i + 1] = A[i + 1] - Y[i]

print(A_sum_1 - sum(A))


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

  else:
    A[i] = A[i] - Y[i]
    Y[i] = 0

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

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

input = sys.stdin.readline

def main():
    ans = 0
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    t = 0
    for i in range(n):
        for j in range(i, i+2):
            if a[j] >= b[i]:
                a[j] -= b[i]
                ans += b[i]
                break
            else:
                ans += a[j]
                a[j] = 0
                b[i] -=  a[j]
    print(ans)

if __name__ == '__main__':
    main()

Fixed code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    for i in range(n):
        for j in range(i, i+2):
            if a[j] >= b[i]:
                a[j] -= b[i]
                ans += b[i]
                break
            else:
                ans += a[j]
                b[i] -=  a[j]
                a[j] = 0
    print(ans)

if __name__ == '__main__':
    main()
Buggy code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

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

print(taoshita)

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

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

print(taoshita)
Buggy code:
n = int(input())
A=[]
for x in input().split():
    A.append(int(x))

B=[]
for x in input().split():
    B.append(int(x))

B.append(int(0))    

TOT = sum ( A )

print ( A )

for i in range (n) :
    KILL = min ( A[i], B[i] )
    A [i] = A [i] - KILL
    A [i+1] = A[i+1] - min (A[i+1], B[i] - KILL)
    print ( A )    
    
print ( TOT - sum ( A ) )

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

B=[]
for x in input().split():
    B.append(int(x))

B.append(int(0))    

TOT = sum ( A )

for i in range (n) :
    KILL = min ( A[i], B[i] )
    A [i] = A [i] - KILL
    A [i+1] = A[i+1] - min (A[i+1], B[i] - KILL)
    
print ( TOT - sum ( A ) )
Buggy code:
N = int(input())
C = tuple(map(int,input().split()))
B = list(map(int,input().split()))
A = list(C)
for i in range(N)[::-1]:
    if A[i+1] > B[i]:
        A[i+1] -= B[i]
    elif B[i] < A[i]+A[i+1]:
        A[i+1] = 0
        A[i] -= B[i] - A[i+1]
    else:
        A[i] = 0
        A[i+1] = 0
ans = 0
for j in range(N+1):
    ans += C[j]-A[j]
print(ans)

Fixed code:
N = int(input())
C = tuple(map(int,input().split()))
B = list(map(int,input().split()))
A = list(C)
for i in range(N)[::-1]:
    if A[i+1] > B[i]:
        A[i+1] -= B[i]
    elif B[i] < A[i]+A[i+1]:
        A[i] -= (B[i] - A[i+1])
        A[i+1] = 0
    else:
        A[i] = 0
        A[i+1] = 0
ans = 0
for j in range(N+1):
    ans += C[j]-A[j]
print(ans)
Buggy code:
n = int(input())
lia = list(map(int,input().split()))
lib = list(map(int,input().split()))
mon = sum(lia)
for i in range(n):
    k = lia[i] - lib[i]
    if k <= 0:
        lia[i] = 0
        lia[i+1] += k
        if lia[i+1] < 0:
            lia[i+1] = 0
print(mon-sum(lia))

Fixed code:
n = int(input())
lia = list(map(int,input().split()))
lib = list(map(int,input().split()))
mon = sum(lia)
for i in range(n):
    k = lia[i] - lib[i]
    if k <= 0:
        lia[i] = 0
        lia[i+1] += k
        if lia[i+1] < 0:
            lia[i+1] = 0
    else:
        lia[i] -= lib[i]
print(mon-sum(lia))
Buggy code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
for i in range(n-1):
    c=min(a[i],b[i])
    a[i]-=c
    b[i]-=c
    d=min(b[i],a[i+1])
    a[i+1]-=d
    ans+=c+d
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):
    c=min(a[i],b[i])
    a[i]-=c
    b[i]-=c
    d=min(b[i],a[i+1])
    a[i+1]-=d
    ans+=c+d
print(ans)
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]:
        if a[i]+a[i+1] <= b[i]:
            c += a[i]+a[i+1]
            a[i+1] = 0
        else:
            c += a[i]
            a[i+1] -= (b[i]-a[i])
    elif a[i] >= b[i]:
        c += b[i]
    
print(c)


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

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

count = 0

for num in range(N):
    if A[num] >= B[num]:
        num_dead_monster = B[num]
    else:
        num_dead_monster = A[num]
    #print("now index", num_dead_monster)
    count += num_dead_monster
    A[num] -= num_dead_monster
    B[num] -= num_dead_monster
    
    if B[num] >= 1:
        if A[num+1] >= B[num]:
            num_dead_monster = B[num]
        else:
            num_dead_monster = A[num+1]
        count += num_dead_monster
        A[num+1] -= num_dead_monster
        #print("next index", B[num])
    
    #print("A", A)
    #print("B", B)
    #print()

print("count", count)

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

count = 0

for num in range(N):
    if A[num] >= B[num]:
        num_dead_monster = B[num]
    else:
        num_dead_monster = A[num]
    #print("now index", num_dead_monster)
    count += num_dead_monster
    A[num] -= num_dead_monster
    B[num] -= num_dead_monster
    
    if B[num] >= 1:
        if A[num+1] >= B[num]:
            num_dead_monster = B[num]
        else:
            num_dead_monster = A[num+1]
        count += num_dead_monster
        A[num+1] -= num_dead_monster
        #print("next index", B[num])
    
    #print("A", A)
    #print("B", B)
    #print()

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):
  ans += min(A[i], B[i])
  A[i+1] -= max(0, B[i]-A[i])

if A[-1] > 0:
  ans += max(0,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] >= B[i]:
        ans += B[i]
        A[i] -= B[i]
    else:
        ans += A[i]
        B[i] -= A[i]
        A[i] = 0
        if A[i + 1] >= B[i]:
            ans += B[i]
            A[i + 1] -= B[i]
        else:
            ans += A[i + 1]
            B[i] -= A[i + 1]
            A[i + 1] = 0
print(ans)