qyliang/CodeNanoFix huggingface dataset:
Buggy code:
#C
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b.append(0)
m = [0 for _ in range(n+1)]
tmp = 0

for i in range(n+1):
    if tmp >= a[i]:
        m[i] += a[i]
        tmp = b[i]
        
    elif tmp < a[i]:
        if a[i] <= b[i]:
            b[i] -= a[i]
            m[i] += a[i]
            tmp = b[i]
        
        else:#a > b
            m[i] += b[i]
            b[i] = 0
            tmp = 0
     
     
print(sum(m))

Fixed code:
#C
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b.append(0)
m = [0 for _ in range(n+1)]
tmp = 0

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

Fixed code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = sum(a)
for i in range(n):
    if a[i]<=b[i]:
        a[i], b[i] = 0, b[i]-a[i]
        a[i+1] = max(a[i+1]-b[i], 0)
    else:
        a[i] -= b[i]
print(x-sum(a))
Buggy code:
n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))

result = 0
sum_a = sum(a)
for i in range(n):
    if a[i] <= b[i]:
        b[i] -= a[i]
        a[i] = 0
        if a[i+1] <= b[i]:
            a[i+1] = 0
        else:
            a[i+1] -= b[i]
    else:
        a[i] -= b[i]
    print('a',a)
    print('b',b)
print(sum_a-sum(a))

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

sum_a = sum(a)
for i in range(n):
    if a[i] <= b[i]:
        b[i] -= a[i]
        a[i] = 0
        if a[i+1] <= b[i]:
            a[i+1] = 0
        else:
            a[i+1] -= b[i]
    else:
        a[i] -= b[i]
print(sum_a-sum(a))
Buggy code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

n = I()
a = LI()
b = LI()

ans = 0
m = 0
for i in range(n):
    if a[i]>=m:
        ans += m
        a[i] -= m
    else:
        ans += a[i]
        a[i]=0
    if a[i]>=b[i]:
        a[i]-b[i]
        ans += a[i]
        m = 0
    else:
        ans += a[i]
        m = b[i] - a[i]
if a[n]>=m:
    ans +=m
else:
    ans +=a[n]
print(ans)

Fixed code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

n = I()
a = LI()
b = LI()

ans = 0
m = 0
for i in range(n):
    if a[i]>=m:
        ans += m
        a[i] -= m
    else:
        ans += a[i]
        a[i]=0
    if a[i]>=b[i]:
        ans += b[i]
        m = 0
    else:
        ans += a[i]
        m = b[i] - a[i]
    # print(m,ans)
if a[n]>=m:
    ans +=m
else:
    ans +=a[n]
print(ans)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
print(b)
sum = 0
attack = 0
mon = a[0]
for i in range(n):
    if mon >= b[i]:
        sum += b[i]
        mon = a[i+1]
    else:
        sum += mon
        attack = b[i] - mon
        mon = max(0, a[i+1] - attack)
        sum += (a[i+1] - mon)
print(sum)


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

M = list(map(int, input().split()))

Y = list(map(int, input().split()))

sum_M = []
for i in range(0, N+1):
 if i == 0:
  p1 = max(0, Y[0] - M[0])
  sum_M.append(min(M[0], Y[0]) ) 
 elif i < N:
  sum_M.append(min(M[i], Y[i] + p1) )
  p0 = 
  p1 = min(Y[i], max(0, Y[i] - (M[i] + p1)) )
 else:
  sum_M.append(min(M[i], p1) )
  p1 = max(0, p1 - M[i])  

print(sum(sum_M))


Fixed code:
N = int(input())

M = list(map(int, input().split()))

Y = list(map(int, input().split()))

sum_M = []
for i in range(0, N+1):
 if i == 0:
  p1 = max(0, Y[0] - M[0])
  sum_M.append(min(M[0], Y[0]) ) 
 elif i < N:
  sum_M.append(min(M[i], Y[i] + p1) )
  p1 = min(Y[i], max(0, Y[i] - M[i] + p1) )
 else:
  sum_M.append(min(M[i], p1) )
  p1 = max(0, p1 - M[i])  

print(sum(sum_M))
Buggy code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
count = 0
flag = 0
i = 0
while(i<len(a)-1):
	if b[i]<a[i]+a[i+1]:
		count = count+b[i]
		a[i+1] = a[i]+a[i+1]-b[i]
		flag = 0
		i += 1

	else:
		if flag == 0:
			count = count+a[i]+a[i+1]
			a[i+1] = 0
			flag = 1
			i = i + 1

		else:
			count = count + a[i+1]
			a[i+1] = 0
			i = i + 1

print(count)


Fixed code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
count = 0
flag = 0
i = 0
while(i<len(a)-1):
	if b[i]<a[i]+a[i+1]:
		count = count+b[i]
		if a[i]<b[i]:
			a[i+1] = a[i]+a[i+1]-b[i]
		flag = 0
		i += 1

	else:
		if flag == 0:
			count = count+a[i]+a[i+1]
			a[i+1] = 0
			flag = 1
			i = i + 1

		else:
			count = count + a[i+1]
			a[i+1] = 0
			i = i + 1

print(count)
Buggy code:
n=int(input())
#monster
a=list(map(int,input().split()))
#blaver
b=list(map(int,input().split()))
print(a,b)
ans=0
for i in range(n):
    if a[i] < b[i]:
        b[i] -= a[i]
        ans += 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)  
    

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

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:
        if b[i]-a[i]<a[i+1]:
            count+=b[i]
            a[i+1]-=b[i]-a[i]
        else:
            count+=a[i]+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):
    b  = B[i]
    a  = A[i]
    a2 = A[i+1]

    if a <= b:
        ans += a
        A[i] = 0
        b -= a
    else:
        ans += b
        A[i] -= b
        b = 0

    if b > 0:
        if a2 <= b:
            ans += a2
            A[i+1] = 0
            b -= a2
        else:
            ans += b
            A[i+1] -= b
            b = 0
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):
    b  = B[i]
    a  = A[i]
    a2 = A[i+1]

    if a <= b:
        ans += a
        A[i] = 0
        b -= a
    else:
        ans += b
        A[i] -= b
        b = 0

    if b > 0:
        if a2 <= b:
            ans += a2
            A[i+1] = 0
            b -= a2
        else:
            ans += b
            A[i+1] -= b
            b = 0
print(ans)
Buggy code:
N = int(input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
L = 0
for i in range(N):
  l = min(A[i], B[i])
  r = min(A[i + 1], B[i])
  A[i + 1] -= r
  L += l + r
print(L)

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

c = 0
for i in range(n):
      ci = 0
      ci = min(a[i],b[i])
      c += ci
      b[i] -= ci
      ci1 = 0
      ci1 = min(a[i+1],b[i])
      c += ci1
      c[i+1] -= ci1

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):
      ci = 0
      ci = min(a[i],b[i])
      c += ci
      b[i] -= ci
      ci1 = 0
      ci1 = min(a[i+1],b[i])
      c += ci1
      a[i+1] -= ci1

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

if b[n-1] - a[n-1] > 0:
    count += a[n-1]
    rest = b[n-1] - a[n-1]
    if rest < a[n]:
        count += rest
    else:
        count += a[n]
else:
    count += b[i]


print(count)

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

if b[n-1] - a[n-1] > 0:
    count += a[n-1]
    rest = b[n-1] - a[n-1]
    if rest < a[n]:
        count += rest
    else:
        count += a[n]
else:
    count += b[n-1]


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

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

killed = 0

for i in range(n):
    if b[i] <= a[i]:
        killed += b[i]
        continue
    else:
        b[i] -=	a[i]
        killed += a[i] + min(a[i+1], b[i])
	a[i+1] = max(0, a[i+1]-b[i])

print(killed)


Fixed code:
# -*- coding: utf-8 -*-                                                                              

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

killed = 0

for i in range(n):
    if b[i] <= a[i]:
        killed += b[i]
        continue
    else:
        b[i] -=	a[i]
        killed += a[i] + min(a[i+1], b[i])
        a[i+1] = max(0, a[i+1]-b[i])

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

Fixed code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
count = 0
for i in range(N):
  c1 = min(A[i], B[i])
  c2 = min(A[i+1], max(0, B[i]-A[i]))
  A[i+1] -= c2
  count += c1+c2
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]>=b[i]):
        ans+=b[i]
    elif(a[i+1]<b[i]-a[i])
        ans+=a[i]+a[i+1]
        a[i+1]=0
    else:
        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(a[i]>=b[i]):
        ans+=b[i]
    elif(a[i+1]<b[i]-a[i]):
        ans+=a[i]+a[i+1]
        a[i+1]=0
    else:
        ans+=b[i]
        a[i+1]-=b[i]-a[i]
print(ans)
Buggy code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
ans = 0
for i in range(n):
  if a[i] + a[i+1] <= b[i]:
    s += a[i] + a[i+1]
    a[i+1] = 0
  elif a[i] + a[i+1] > b[i] and a[i] <= b[i]:
    s += b[i]
    a[i+1] -= b[i] - a[i]
  elif a[i] > b[i]:
    s += a[i]
print(s)
    
    

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

count = 0
next_b = 0
for i, ai in enumerate(a):
  if i < n:
    bi = b[i]
    if ai > next_b:
      ai = ai - next_b
      count += next_b
    else:
      ai = 0
      next_b = next_b -ai  
      count += ai
    if ai < bi:
      count += ai
      next_b = bi -ai
    else:
      count += bi
      next_b = 0
  else:
    if ai > next_b:
      count += next_b
    else:
      count += ai
      
print(count)

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

count = 0
next_b = 0
for i, ai in enumerate(a):
  #print(ai, next_b, count)
  if i < n:
    bi = b[i]
    if ai > next_b:
      ai = ai - next_b
      count += next_b
    else:
      count += ai
      ai = 0
      
    if ai < bi:
      count += ai
      next_b = bi -ai
    else:
      count += bi
      next_b = 0
  else:
    if ai > next_b:
      count += next_b
    else:
      count += ai
      
print(count)
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 B[i] >= A[i]:
    s = B[i] - A[i]
    count += A[i]
    A[i] = 0
    if s >= A[i + 1]:
      count += A[i + 1]
      A[i + 1] = 0
    else:
      count += s
      A[i + 1] -= s
  else:
    count += B[i]
    A[i] -= B[i]

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]:
    s = B[i] - A[i]
    count += A[i]
    A[i] = 0
    if s >= A[i + 1]:
      count += A[i + 1]
      A[i + 1] = 0
    else:
      count += s
      A[i + 1] -= s
  else:
    count += B[i]
    A[i] -= B[i]
print(count)
Buggy code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
ans = 0
for i in range(n):
  if a[i]>=b[i]: ans+=b[i]
  else:
    a[i+1] = max(0,a[i]+a[i+1]-b[i])
    ans+=min(b[i],a[i]+a[i+1])
print(ans)

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

s1 = sum(a)
for i in range(n + 1):
    if i == 0:
        if a[i] < b[i]:
            b[i] -= a[i]
            a[i] = 0
        else:
            a[i] -= b[i]
            b[i] = 0
    elif i == n:
        if a[i] < b[i - 1]:
            a[i] = 0
        else:
            a[i] -= b[i - 1]
            b[i] = 0
    else:
        if a[i] < b[i - 1]:
            a[i] = 0
        else:
            a[i] -= b[i - 1]
        if a[i] < b[i]:
            b[i] -= a[i]
            a[i] = 0
        else:
            a[i] -= b[i]
            b[i] = 0
print(s1 - sum(a))


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

s1 = sum(a)
for i in range(n + 1):
    if i == 0:
        if a[i] < b[i]:
            b[i] -= a[i]
            a[i] = 0
        else:
            a[i] -= b[i]
            b[i] = 0
    elif i == n:
        if a[i] < b[i - 1]:
            a[i] = 0
        else:
            a[i] -= b[i - 1]
            b[i - 1] = 0
    else:
        if a[i] < b[i - 1]:
            a[i] = 0
        else:
            a[i] -= b[i - 1]
        if a[i] < b[i]:
            b[i] -= a[i]
            a[i] = 0
        else:
            a[i] -= b[i]
            b[i] = 0
print(s1 - sum(a))
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 b[i] >= a[i]:
    c += a[i]
  elif b[i]+b[i+1] >= a[i]:
    c += a[i]
    b[i+1] -= a[i]-b[i]
  else:
    c += b[i]+b[i+1]
    b[i+1] = 0
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]:
    c += b[i]
  elif a[i]+a[i+1] >= b[i]:
    c += b[i]
    a[i+1] -= b[i]-a[i]
    continue
  else:
    c += a[i]+a[i+1]
    a[i+1]=0
print(c)
Buggy code:
from sys import stdin
n = int(stdin.readline().rstrip())
A = list(map(int,stdin.readline().rstrip().split()))
B = list(map(int,stdin.readline().rstrip().split()))
point = 0
fa = min(A[0],B[0])
fb = min(A[1],B[0]-f00)
point += fb
point += fa
for i in range(1,n):
    fc = min(A[i]-fb,B[i])
    point += fc
    fb = min(A[i+1],B[i]-fc)
    point += fb
print(point)

Fixed code:
from sys import stdin
n = int(stdin.readline().rstrip())
A = list(map(int,stdin.readline().rstrip().split()))
B = list(map(int,stdin.readline().rstrip().split()))
point = 0
fa = min(A[0],B[0])
fb = min(A[1],B[0]-fa)
point += fb
point += fa
for i in range(1,n):
    fc = min(A[i]-fb,B[i])
    point += fc
    fb = min(A[i+1],B[i]-fc)
    point += fb
print(point)
Buggy code:
def solve(string):
    n, *ab = map(int, string.split())
    a, b = ab[:n + 1], ab[n + 1:]
    _sum = sum(a)
    for i, _b in enumerate(b[::-1]):
        if a[n - i] < _b:
            _b -= a[n - i]
            a[n - i] = 0
            a[n - i - 1] = max(0, a[n - i - 1] - _b)
        else:
            a[n - i] -= _b
    return str(_sum - sum(a))


if __name__ == '__main__':
    n = int(input())
    print(solve('{}\n'.format(n) + '\n'.join([input() for _ in range(n)])))


Fixed code:
def solve(string):
    n, *ab = map(int, string.split())
    a, b = ab[:n + 1], ab[n + 1:]
    _sum = sum(a)
    for i, _b in enumerate(b[::-1]):
        diff = min(a[n - i], _b)
        _b -= diff
        a[n - i] -= diff
        a[n - i - 1] = max(0, a[n - i - 1] - _b)
    return str(_sum - sum(a))


if __name__ == '__main__':
    n = int(input())
    print(solve('{}\n'.format(n) + '\n'.join([input() for _ in range(2)])))
Buggy code:
n=int(input())
a=list(map(input().split()))
b=list(map(input().split()))
cnt=0
for i in range(0,n-2):
  if b[i]>=b[i+1]:
    if b[i]<=a[i+1]:
      cnt+=b[i]
      a[i+1]-=b[i]
    else:
      cnt+=a[i+1]
      cnt+=min(a[i],(b[i]-a[i+1]))
  else:
    if b[i]<=a[i]:
      cnt+=b[i]
    else:
      cnt+=a[i]
      a[i+1]-=b[i]-a[i]
      
cnt+=min(a[n],b[n-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(0,n):
  if a[i]>=b[i]:
    cnt+=b[i]
    b[i]=0
  else:
    cnt+=a[i]
    if b[i]-a[i]<=a[i+1]:
      cnt+=b[i]-a[i]
      a[i+1]-=(b[i]-a[i])
    else:
      cnt+=a[i+1]
      a[i+1]=0
 
print(cnt)
Buggy code:
N = int(input())
mons = list(map(int, input().split()))
yush = list(map(int, input().split()))

temp = sum(mons)

for i, yu in enumerate(yush):
    mo = mons[i]
    if (yu >= mo):
        mons[i] = 0
        yush[i] = yu - mo
        
        if (mons[i + 1] > yush[i]):
            mons[i + 1] -= yush[i]
        else:
            monx[i + 1] = 0
        
    else:
        mons[i] = mo - yu
        yush[i] = 0
ans = temp - sum(mons)
print(ans)

Fixed code:
N = int(input())
mons = list(map(int, input().split()))
yush = list(map(int, input().split()))

temp = sum(mons)

for i, yu in enumerate(yush):
    mo = mons[i]
    if (yu >= mo):
        mons[i] = 0
        yush[i] = yu - mo
        
        if (mons[i + 1] > yush[i]):
            mons[i + 1] -= yush[i]
        else:
            mons[i + 1] = 0
        
    else:
        mons[i] = mo - yu
ans = temp - sum(mons)
print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0
p = 0

for index, i in enumerate(A):
  if index > len(B) - 1:
    if p > A[index]:
      ans += A[index]
    else:
      print(p)
      
    break

  if A[index] - (B[index] + p) < 0:
    ans += A[index]
    p = abs(A[index] - B[index] - p)
    continue

  ans += B[index]
  p = 0

print(ans)

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

ans = 0
p = 0

for index, i in enumerate(A):
  if index > len(B) - 1:
    if p > A[index]:
      ans += A[index]
    else:
      ans += p
      
    break

  if A[index] - (B[index] + p) < 0:
    ans += A[index]
    if p > A[index]:
      p = B[index]
    else:
      p = abs(A[index] - B[index] - p)
      
    continue

  ans += B[index] + p
  p = 0

print(ans)
Buggy code:
import sys
input = sys.stdin.readline
    
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 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)

Fixed code:
import sys
input = sys.stdin.readline
    
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 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()))
ans = 0
for i in range(N):
    if A[i] >= B[i]:
        ans += B[i]
    if 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 A[i] >= B[i]:
        ans += B[i]
    if A[i] < B[i]:
        ans += A[i]
        if A[i+1] >= (B[i] - A[i]):
            A[i+1] -= B[i] - A[i]
            ans += B[i] - A[i]
        else:
            ans += A[i+1]
            A[i+1] = 0
print(ans)