Problem:
 Score : 300 points    Problem Statement There are N+1 towns. The i -th town is being attacked by A_i monsters.  We have N heroes. The i -th hero can defeat monsters attacking the i -th or (i+1) -th town, for a total of at most B_i monsters.  What is the maximum total number of monsters the heroes can cooperate to defeat?      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq A_i \leq 10^9  1 \leq B_i \leq 10^9         Input Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_{N+1}  B_1  B_2  ...  B_N       Output Print the maximum total number of monsters the heroes can defeat.        Sample Input 1 2
3 5 2
4 5      Sample Output 1 9  If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.   The first hero defeats two monsters attacking the first town and two monsters attacking the second town.  The second hero defeats three monsters attacking the second town and two monsters attacking the third town.        Sample Input 2 3
5 6 3 8
5 100 8      Sample Output 2 22       Sample Input 3 2
100 1 1
1 100      Sample Output 3 3

Solution (Python):
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
    elif A[i] >= B[i]:
        Ans += B[i]
    else:
        Ans += B[i]
        A[i+1] -= B[i] - A[i]
print(Ans)

Solution (Python):
N = int(input())
monster = list(map(int, input().split()))
hero = list(map(int, input().split()))
ans = 0

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

print(ans)

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

Solution (Python):
n = int(input())
list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))
ans = 0
for i in range(n):
  if list_A[i] >= list_B[i]:
    list_A[i] -= list_B[i]
    ans += list_B[i]
    list_B[i] = 0
  else:
    ans += list_A[i]
    list_B[i] -= list_A[i]
    list_A[i] = 0
    if list_B[i] > list_A[i+1]:
      ans += list_A[i+1]
      list_A[i+1] = 0
    else:
      ans += list_B[i]
      list_A[i+1] -= list_B[i]
print(ans)

Solution (Python):

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


print(ans)

Solution (Python):
n=int(input())
a=[int(x) for x in input().rstrip().split()]
b=[int(x) for x in input().rstrip().split()]
ans=0
for i in range(len(b)):
  if a[i]+a[i+1]<=b[i]:
    ans+=a[i]+a[i+1]
    a[i]=0
    a[i+1]=0
  
  elif a[i]<=b[i]:
    ans+=a[i]
    ans+=(b[i]-a[i])
    a[i+1]=a[i+1]-(b[i]-a[i])
  else:
    a[i]-=b[i]
    ans+=b[i]
print(ans)

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

Solution (Python):
N = int(input())
A = list(input().split())
B = list(input().split())


def Levonas(a,b):
    if a-b < 0:
        return (0,b-a,a)
    else:
        return (a-b, 0, b)

i = 0
j = N-1
su = 0

while i <= j:
    te1 = Levonas(int(A[i]), int(B[i]))
    A[i] = te1[0]
    B[i] = te1[1]
    su = su + te1[2]
    te2 = Levonas(int(A[i+1]), int(B[i]))
    A[i+1] = te2[0]
    B[i] = te2[1]
    su = su + te2[2]
    i = i+1
    
    
    
    te3 = Levonas(int(A[j+1]), int(B[j]))
    A[j+1] = te3[0]
    B[j] = te3[1]
    su = su + te3[2]
    te4 = Levonas(int(A[j]), int(B[j]))
    A[j] = te4[0]
    B[j] = te4[1]
    su = su + te4[2]
    j = j - 1
    
    
    
    
while i <= j:
    if int(A[i])-int(B[i]) < 0:
        B[i] = int(B[i])-int(A[i])
        su = su + int(A[i])
        A[i] = 0
        if int(A[i+1])-int(B[i]) < 0:
            B[i] = int(B[i])-int(A[i+1])
            su = su + int(A[i+1])
            A[i+1] = 0
        else:
            su = su + int(B[i])
            A[i+1] = int(A[i+1])-int(B[i])
            B[i] = 0
    else:
        su = su + int(B[i])
        B[i] = 0
    i = i+1
    print(B)
    if int(A[j+1])-int(B[j]) < 0:
        B[j] = int(B[j])-int(A[j+1])
        su = su + int(A[j+1])
        A[j]=0
        if int(A[j])-int(B[j]) < 0:
            B[j] = int(B[j])-int(A[j])
            su = su + int(A[j])
            A[j] = 0
        else:
            su = su + int(B[j])
            A[j] = int(A[j])-int(B[j])
            B[j] = 0
        
    else:
        su = su + int(B[j])
        B[j] = 0
    j = j-1
    print(B)

print(su)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
viran = 0
for i in range(n):
    if a[i] + a[i + 1] - b[i] >= 0:
        viran += b[i]
        if a[i] > b[i]:
            a[i] -= b[i]
        else:
            b[i] -= a[i]
            a[i] = 0
            a[i + 1] -= b[i]
        b[i] = 0
    else:
        viran += a[i] + a[i + 1]
        a[i] = 0
        a[i + 1] = 0
        b[i] -= a[i] + a[i + 1]
print(viran)

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

kills = 0
b_k = 0

for i in range(n):
    b_k = b[i]
    for j in range(2):
        if b_k - a[i+j] >= 0:
            kills += a[i+j]
            b_k -= a[i+j]
            a[i+j] = 0
        else:
            kills += b_k
            a[i+j] -= b_k
            b_k = 0

print(kills)

Solution (Python):
f=lambda:map(int,input().split())
input()
p,*A=f()
B=f()
s=0
for a,b in zip(A,B):
  t=min(b,a+p)
  p=a-max(0,t-p)
  s+=t
print(s)

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

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

print(ans)

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

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

print(ans)

Solution (Python):






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] = 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)

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

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

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

print(count)

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *AB = map(int, read().split())
    A = AB[: N + 1]
    B = AB[N + 1 :]

    M = sum(A)

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

        A[i + 1] = max(A[i + 1] - B[i], 0)

    print(M - sum(A))
    return


if __name__ == '__main__':
    main()

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

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

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
pre=sum(A)

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
cnt=0
for i in range(n):
  f_1 = min(a[i],b[i])
  f_2 = max(0,min(a[i+1],b[i]-a[i]))
  cnt += f_1+f_2
  a[i] -= f_1
  a[i+1] -= f_2
print(cnt)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cnt = 0
beforeKill = 0
for i in range(n):
    kill1 = min(A[i] - beforeKill, B[i])
    kill2 = min(A[i+1], B[i] - kill1)
    cnt += kill1 + kill2
    beforeKill = kill2
print(cnt)

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


print(cnt)

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

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

if a[-1]>=overpower:
    print(ans+overpower)
else:
    print(ans+a[-1])

Solution (Python):
import math

n = int(input())
aL = list(map(int, input().split(" ")))
bL = list(map(int, input().split(" ")))

aSum = sum(aL)

for i in range(n):
    dmg1 = min(aL[i], bL[i])
    aL[i] -= dmg1
    bL[i] -= dmg1

    dmg2 = min(aL[i + 1], bL[i])
    aL[i + 1] -= dmg2
    bL[i] -= dmg2

print(aSum - sum(aL))

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

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

print(monster - sum(A))

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

Solution (Python):
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]
        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)

Solution (Python):
def main():
    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]:
            a[i] = a[i] - b[i]
            c += b[i]
            b[i] = 0
        else:
            c += a[i]
            b[i] = b[i] - a[i]
            a[i] = 0
            if a[i+1] >= b[i]:
                a[i+1] = a[i+1] - b[i]
                c += b[i]
                b[i] = 0
            else:
                c += a[i+1]
                b[i] = b[i] - a[i+1]
                a[i+1] = 0
    print(c) 
           
if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
sm=0
c=0
for i in range(n):
  if c >=a[i]:
    sm+=a[i]
    c=b[i]
  elif b[i]+c>=a[i]:
    sm+=a[i]
    c=-a[i]+c+b[i]
  else:
    sm+=b[i]+c
    c=0
sm+=min(c,a[-1])
print(sm)

Solution (Python):




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

print(ans)

Solution (Python):
n = int(input())
enemy  = list(map(int,input().split()))
hero  = list(map(int,input().split()))

ans = 0

for i in range(n):
    if hero[i] > enemy[i]:
        if enemy[i+1] < (hero[i]-enemy[i]):
            ans += (enemy[i]+enemy[i+1])
            enemy[i+1] = 0
        else:
            enemy[i+1] -= (hero[i]-enemy[i])
            ans += hero[i]
    else:
        ans += hero[i]

print(ans)

Solution (Python):
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
answer_list = [0]*N

for i in range(N):
    temp = B_list[i]

    for j in range(i, i+2):
        if temp - A_list[j] <= 0:
            answer_list[i] += temp
            A_list[j] -= temp
            break
        else:
            answer_list[i] += A_list[j]
            temp -= A_list[j]
            A_list[j] -= A_list[j]

print(sum(answer_list))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
x=sum(b)
for i in range(n):
  s=a[i]+a[i+1]
  t=b[i]
  x-=max(0,t-s)
  a[i+1]-=max(0,b[i]-a[i])
  a[i+1]=max(a[i+1],0)
print(x)

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

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
sm=sum(a)

cnt=0
rest=0

for i in range(n):
    a[i]=max(0,a[i]-rest)
    rest=0
    rest=max(0,b[i]-a[i])
    a[i]=max(0,a[i]-b[i])
a[n]=max(0,a[n]-rest)

print(sm-sum(a))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

i, j = 0, 0
count = sum(a)

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

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

print(count)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
for i in range(n):
    if b[i] <= a[i]:
        cnt += b[i]
    else :
        cnt += a[i]
        c = b[i] - a[i]
        if c <= a[i+1]:
            cnt += c
            a[i+1] -= c
        else :
            cnt += a[i+1]
            a[i+1] = 0
print(cnt)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

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

b += [0]

ans = 0

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

Solution (Python):
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]
      a[i+1]=0
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
for i in range(N):
    a=min(A[i],B[i])
    B[i]-=a
    b=min(A[i+1],B[i])
    A[i+1]-=b
    ans+=a+b
print(ans)

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

res = 0
for i in range(len(B)):
  res += min(A[i], B[i])
  rem = B[i] - A[i]
  if rem > 0 and i+1 < len(A):
    res += min(A[i+1], rem)
    A[i+1] = max(0, A[i+1]-rem)

print(res)

Solution (Python):
def main():
    n = int(input())
    a=[int(i) for i in input().split()]
    b=[int(i) for i in input().split()]

    ind = 0
    res = 0

    for i in range(n):
        if a[i] <= b[i]:
            res += a[i]
            k = b[i]-a[i]
            res += min(k,a[i+1])

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

        else:
            res += b[i]
    print(res)



if __name__ == '__main__':
    main()

Solution (Python):
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=ans+b[i]
    elif b[i]<=a[i]+a[i+1]:
        ans=ans+b[i]
        a[i+1]=a[i]+a[i+1]-b[i]
    else:
        ans=ans+a[i]+a[i+1]
        a[i+1]=0
print(ans)

Solution (Python):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.buffer.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))

n = ri()
a = rl()
b = rl()
cnt = 0
for i in range(n):
  ai = a[i]
  bi = b[i]
  if bi > ai:
    cnt += ai
    bi -= ai
    ai1 = a[i+1]
    if bi > ai1:
      cnt += ai1
      a[i+1] = 0
    else:
      a[i+1] -= bi
      cnt += bi
  else:
    cnt += bi
print(cnt)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split())) + [0]

bonus = 0
x = 0
for i in range(n+1):
    enemy = max(0, a[i] - bonus) 
    tmp = enemy - b[i]
    if tmp <= 0:
        x += a[i]
        bonus = -tmp
    else:
        x += (b[i] + bonus)
        bonus = 0

print(x)

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

M = sum(A)

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

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
 
sa = sum(a)
 
for i,b in enumerate(b):
	c = min(a[i],b)
	a[i] -= c
	a[i+1] -= min(a[i+1],b-c)
 
print(sa-sum(a))

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

 
sm = sum(a)
for i in range(n):
	c = min(a[i],b[i])
	a[i] -= c
	a[i+1] -= min(a[i+1],b[i]-c)
	
print(sm-sum(a))

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

sa = sum(a)

for i,b in enumerate(b):
	c = min(a[i],b)
	a[i] -= c
	a[i+1] -= min(a[i+1],b-c)

print(sa-sum(a))

Solution (Python):
import sys

input = lambda: sys.stdin.readline().rstrip()


def solve():

    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    asum = 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]:
                B[i] -= A[i + 1]
                A[i + 1] = 0
            else:
                A[i + 1] -= B[i]
                B[i] = 0
        else:
            A[i] -= B[i]
            B[i] = 0

    ans = asum - sum(A)
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
c = 0
for n in range(N):
  if n == 0:
    c += min(A[0], B[0])
    temp = c
  else:
    c += min(A[n] - temp, B[n])
    temp = min(A[n] - temp, B[n])
  c += min(A[n+1], B[n] - temp)
  temp = min(A[n+1], B[n] - temp)
print(c)

Solution (Python):
n = int(input())
apple = list(map(int, input().split()))
orange = list(map(int, input().split()))
count = 0

for i in range(n):
    if orange[i] >= apple[i]:
        a = orange[i] - apple[i]
        count += apple[i]
        if apple[i+1] >= a and a != 0:
            b = apple[i + 1] - a
            apple[i+1] = b
            count += a
        elif apple[i + 1] < a and a != 0:
            count += apple[i + 1]
            apple[i + 1] = 0
        else:
            pass
    else:
        count += orange[i]
        apple[i] = 0
print(count)

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

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

print(all-sum(a))

Solution (Python):
def main():
    n = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

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

        if Bs[i] >= As[i]:
            Bs[i] -= As[i]
            ans += As[i]
            As[i] = 0
        else:
            As[i] -= Bs[i]
            ans += Bs[i]
            Bs[i] = 0
    print(ans)


if __name__ == "__main__":
    main()

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

before_sumA=sum(A)
before_sumB=sum(B)
for i in range(N):
    if A[i]>B[i]:
        R=A[i]
        A[i]-=B[i]
        B[i]=0
    elif A[i]==B[i]:
        R=A[i]
        A[i]=0
        B[i]=0
    elif A[i]+A[i+1]>B[i]:
        R=A[i]
        A[i]=0
        B[i]-=R
        A[i+1]-=B[i]
        B[i]=0
    else:
        R=A[i]+A[i+1]
        A[i]=0
        A[i+1]=0
        B[i]-=R

after_sumA=sum(A)
after_sumB=sum(B)

print(before_sumB-after_sumB)

Solution (Python):
import math
import re
import copy
import sys

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

for i in range(n):
    c += min(a[i],b[i])
    if a[i] < b[i]:
        c += min(a[i+1],b[i]-a[i])
        a[i+1] -= min(a[i+1],b[i]-a[i])

print(c)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))[::-1]
b=list(map(int,input().split()))[::-1]
ans=0
for i in range(n):
  c=max(b[i]-a[i],0)
  ans+=min(b[i],a[i])
  ans+=min(c,a[i+1])
  a[i+1]=max(a[i+1]-c,0)
  
print(ans)

Solution (Python):
from collections import deque
N = int(input())
A = deque(map(int, input().split()))
B = deque(map(int, input().split()))
ans = 0
tmp = A.popleft()
while A:
  b = B.popleft()
  nxt = A.popleft()
  ans += min(b, tmp + nxt)
  if b >= tmp + nxt:
    tmp = 0
  elif tmp + nxt > b >= tmp:
    tmp = tmp + nxt - b
  else:
    tmp = nxt
print(ans)

Solution (Python):
import sys
import math
from collections import Counter
import itertools
import fractions
import datetime
from decimal import Decimal



def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())

def LI(): return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'


p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")



N=II()
A=LI()
B=LI()

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.append(0)
ans = 0
for i in range(n + 1):
    m = min(a[i], b[i - 1])
    a[i] -= m
    b[i - 1] -= m
    ans += m
    m = min(a[i], b[i])
    a[i] -= m
    b[i] -= m
    ans += m
print(ans)

Solution (Python):

N = int(input())

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

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

count = 0
for i in range(N):
  
  
  if(A_list[i] >= B_list[i]):
    count = count + B_list[i]
    
    
  
  
  elif((B_list[i] - A_list[i]) < A_list[i+1]):
    count = count + A_list[i] + (B_list[i] - A_list[i])
    A_list[i+1] = A_list[i+1] - (B_list[i] - A_list[i])
    
    
  
  else:
    count = count + A_list[i+1]+A_list[i]
    
    A_list[i+1] = 0
    
  
    
print(count)

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

print(total)

Solution (Python):
def main():
    n=int(input())
    a=list(map(int, input().split()))
    b=list(map(int, input().split()))
    
    al = sum(a)
    for i in range(n):
        a[i] -= b[i]
        if a[i] < 0:
            a[i+1] += max(-a[i+1], a[i])
            a[i] = 0
    print(al-sum(a))
    
if __name__ == "__main__":
    main()

Solution (Python):

N = int(input()) + 1
A = list(map(int,input().split()))
B = list(map(int,input().split())) + [0]
win = 0
remain = 0
for n in range(N):
    if A[n] < remain:
        win += A[n]
        A[n] = 0
    else:
        win += remain
        A[n] -= remain

    if A[n] < B[n]:
        win += A[n]
        remain = B[n] - A[n]
    else:
        win += B[n]
        remain = 0
print(win)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
s = sum(A)

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

print(s-sum(A))

Solution (Python):
n=int(input())
mon=list(map(int,input().split()))
yusya=list(map(int,input().split()))
a=sum(mon)
yoryoku=0
for i in range(n):
    
    if mon[i]>=yoryoku:
        mon[i]-=yoryoku
    else:
        mon[i]=0
    if mon[i]>=yusya[i]:
        mon[i]-=yusya[i]
        yoryoku=0
    else:
        yoryoku=yusya[i]-mon[i]
        mon[i]=0
        
mon[-1]-=yoryoku
if mon[-1]<0:
    mon[-1]=0
    
print(a-sum(mon))

Solution (Python):
import collections
n = int(raw_input())
cities,q = collections.deque(map(int, raw_input().split(' '))), collections.deque(map(int, raw_input().split(' ')))
res = 0
while(q):
	u = q.pop()
	for _ in range(2):
		c = cities.pop()
		m = min(u, c)
		c -=m
		u -= m
		res += m
	cities.append(c)
print res

Solution (Python):
import collections
n = int(raw_input())
cities = collections.deque(map(int, raw_input().split(' ')))
q = collections.deque(map(int, raw_input().split(' ')))
res = 0
p = 0
while(q):
	w = q.pop()
	if cities[-1]:
		m = min(w, cities[-1])
		cities[-1]-=m
		w -= m
		res += m
	cities.pop()
	if cities[-1]:
		m = min(w, cities[-1])
		cities[-1]-=m
		w -= m
		res += m
print res

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=sum(a)
for i in range(n):
    if a[i]+a[i+1]>=b[i]:
        if a[i]>=b[i]:
            a[i]-=b[i]
        else:
            a[i+1]=a[i+1]-b[i]+a[i]
            a[i]=0
    else:
        a[i]=0
        a[i+1]=0
print(ans-sum(a))

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

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

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

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

print(monstar - sum(A))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = list(map(int, readline().split()))
b = list(map(int, readline().split()))
monster = sum(a)
for i in range(n):
    if a[i] - b[i] >= 0:
        a[i] -= b[i]
    else:
        a[i+1] = max(a[i+1] - (b[i]-a[i]),0)
        a[i] = 0
print(monster-sum(a))

Solution (Python):
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])
    if B[i] > A[i]:
        carry = (B[i] - A[i])
        if A[i+1] > carry:
            ans += carry
            A[i+1] -= carry
        else:
            ans += A[i+1]
            A[i+1] = 0


print(ans)