CodeEff/ECCO huggingface dataset:
# Original solution
# ABC135C - City Savers
# greedy algorithm
def main():
    n = int(eval(input()))
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = a1 if a1 < b else b
        cnt2 = a2 if a2 < b - cnt1 else b - cnt1
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  # a2 becomes a1 in next turn
    print(ans)


if __name__ == "__main__":
    main()

# Optimized solution
# ABC135C - City Savers
# greedy algorithm
def main():
    n = int(eval(input()))
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = a1 if a1 < b else b
        x = b - cnt1
        cnt2 = a2 if a2 < x else x
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  # a2 becomes a1 in next turn
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
n = int(input())
a = [int(i) for i in input().split(' ')]
b = [int(i) for i in input().split(' ')]
ret = 0
for i in range(n):
    e = min(a[i], b[i])
    ret += e
    a[i] -= e
    b[i] -= e
    e = min(a[i + 1], b[i])
    ret += e
    a[i + 1] -= e
    b[i] -= e

print(ret)


# Optimized solution
N = int(eval(input()))
A = [int(s) for s in input().split(' ')]
B = [int(s) for s in input().split(' ')] + [0]
m = 0
pa = 0
for a, b in zip(A, B):
    m1 = 0
    if pa > 0:
        m1 = min(pa, a)
        a -= m1
    m2 = min(a, b)
    pa = b - m2
    m += m1 + m2

print(m)
# Original solution
n=int(eval(input()))
a=list(map(int, input().split()))
b=list(map(int, input().split()))

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

# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))

ans=0
rmn=0
for i in range(n):
    if a[i]<b[i]+rmn:
        if rmn>a[i]:
            rmn=b[i]
        else:
            rmn=rmn+b[i]-a[i]
        ans+=a[i]
    else:
        ans+=b[i]+rmn
        rmn=0
ans+=min(a[-1],rmn)
print(ans)
# Original solution
def main():
  import sys
  n = int(eval(input()))
  a = tuple(map(int,sys.stdin.readline().split()))
  b = tuple(map(int,sys.stdin.readline().split()))
  
  count=0
  buffer=0
  
  for c,h in zip(a,b):
    count += min(buffer,c)
    g = max(0,c-buffer)
    count += min(h,g)
    buffer=max(0,h-g)
  else:
    count+=min(buffer,a[-1])
    
    
  print(count)
  
if __name__=="__main__":
  main()

# Optimized solution
def main():
  import sys
  n = int(eval(input()))
  a = list(map(int,sys.stdin.readline().split()))
  b = list(map(int,sys.stdin.readline().split()))
  
  count=0
  buffer=0
  
  for c,h in zip(a,b):
    count += min(buffer,c)
    g = max(0,c-buffer)
    count += min(h,g)
    buffer=max(0,h-g)
  else:
    count+=min(buffer,a[-1])
    
    
  print(count)
  
if __name__=="__main__":
  main()
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

c = 0
for i in range(len(b)):
  # print('倒せる数', b)
  # print('倒せる街にいるモンスターの数',a)
  for j in range(b[i]):
    if a[i] > 0:
      b[i] -= 1
      a[i] -= 1
      c += 1
    elif a[i] == 0 and a[i+1] > 0:
      b[i] -= 1
      a[i+1] -= 1
      c += 1

print(c)


# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

c = 0

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

print(c)
# Original solution
import math
import re
import copy
import sys

n = int(eval(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)

# Optimized solution
n = int(eval(input()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
for i in range(n-1,-1,-1):
    ans += min(a[i]+a[i+1],b[i])
    a[i] = max(a[i]-max(b[i]-a[i+1],0),0)
print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
l = A[0]
for i in range(N):
    r = A[i + 1]
    cur = B[i]
    # print(l, r, cur)
    ans += min(l + r, cur)
    l = max(r - max(cur - l, 0), 0)  # めちゃくちゃ整理するとこの形の式になる
print(ans)

# Optimized solution
# https://atcoder.jp/contests/abc135/tasks/abc135_c

N = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
l = A[0]
for i in range(N):
    r = A[i + 1]
    cur = B[i]
    # print(l, r, cur)
    # ans += min(l + r, cur)
    # l = max(r - max(cur - l, 0), 0)  # めちゃくちゃ整理するとこの形の式になる

    # わかりやすくかきなおす
    ans += min(l, cur)
    rest_power = max(cur - l, 0)
    ans += min(rest_power, r)
    l = max(r - rest_power, 0)  # next l

print(ans)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0

for i in reversed(list(range(n+1))):
  if i == n:
    temp = min(a[i], b[i-1])
    ans += temp
    b[i-1] -= temp
  elif i == 0:
    ans += min(a[i], b[i])
  else:
    temp1 = min(a[i], b[i])
    ans += temp1
    a[i] -= temp1
    temp2 = min(a[i], b[i-1])
    ans += temp2
    b[i-1] -= temp2
print(ans)
    

# Optimized solution
n = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0
for i in reversed(list(range(1, n+1))):
    if A[i] <= B[i-1]:
        ans += A[i]
        B[i-1] -= A[i]
        if A[i-1] >= B[i-1]:
            ans += B[i-1]
            A[i-1] -= B[i-1]
        else:
            ans += A[i-1]
            A[i-1] = 0
    else:
        ans += B[i-1]
        B[i-1] = 0
print(ans)
# Original solution
n = int(eval(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)

# Optimized solution
n = int(eval(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)
# Original solution
def solve():
    n = int(eval(input()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        ans += min(B[i], A[i] + A[i + 1])
        A[i + 1] = min(A[i + 1], max(A[i] + A[i + 1] - B[i], 0))
    print(ans)


solve()


# Optimized solution
def solve():
    n = int(eval(input()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        x = A[i] + A[i + 1]
        ans += min(B[i], x)
        A[i + 1] = min(A[i + 1], max(x - B[i], 0))
    print(ans)


solve()
# Original solution
n=int(eval(input()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
sum=0
for i in range(n-1):
    if B[i]-A[i]>0:
        if A[i+1]-(B[i]-A[i])<0:
            
       #     print(A[i]+A[i+1])
            sum+=A[i]+A[i+1]
            A[i+1]=0
        else:
            A[i+1]+=-(B[i]-A[i])
      #      print(B[i])
            sum+=B[i]
    else:
     #   print(B[i])
        sum+=B[i]

if B[n-1]<A[n-1]+A[n]:
    #print(B[n-1])
    sum+=B[n-1]
else:
    #print(A[n-1]+A[n])
    sum+=A[n-1]+A[n]
print(sum)


# Optimized solution
n=int(eval(input()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
sum=0
for i in range(n-1):
    x=B[i]-A[i]
    if x>0:
        if A[i+1]-(x)<0:
            
       #     print(A[i]+A[i+1])
            sum+=A[i]+A[i+1]
            A[i+1]=0
        else:
            A[i+1]+=-(x)
      #      print(B[i])
            sum+=B[i]
    else:
     #   print(B[i])
        sum+=B[i]

if B[n-1]<A[n-1]+A[n]:
    #print(B[n-1])
    sum+=B[n-1]
else:
    #print(A[n-1]+A[n])
    sum+=A[n-1]+A[n]
print(sum)
# Original solution
n = int(eval(input()))
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]
result = 0
for i in range(n):
  if a[i] > b[i]:
    result += b[i]
    a[i] -= b[i]
    b[i] = 0
  else:
    result += a[i]
    b[i] -= a[i]
    a[i] = 0
    if a[i + 1] > b[i]:
      result += b[i]
      a[i + 1] -= b[i]
      b[i] = 0
    else:
      result += a[i + 1]
      b[i] -= a[i + 1]
      a[i + 1] = 0
print(result)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

result = 0

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

print(result)
# Original solution
N = int( eval(input()) )
A, B = list( [int(x) for x in input().split(" ")] ), list( [int(x) for x in input().split(" ")] )
#A = monsters. B = number of to be able to kill monsters.殺せる数

 
def kill_monsters(Ai, Bi):

    Ai -= Bi
    Bi = 0

    if Ai < 0:
        Bi += -1*Ai
        Ai = 0

    return (Ai, Bi)
 
def main(local_A, local_B, lim):
 
    for i in range(lim):
        local_A[i], local_B[i] = kill_monsters(local_A[i], local_B[i])
 
        if 0 < local_B[i]:
            local_A[i+1], local_B[i] = kill_monsters(local_A[i+1], local_B[i])
 
    return [local_A, local_B]

print(( sum(A) - sum(main(A, B, N)[0]) ))

# Optimized solution
def read():
    N = int(eval(input()))
    A = list( map(int, input().split(" ")) )
    B = list( map(int, input().split(" ")) )

    return (N, A, B)

def kill(A, B):
    if B < A:
        A = A - B
        B = 0
    else:
        B = B - A
        A = 0
    
    return (A, B)

def main(N, A, B):

    for i in range(N):
        A[i], B[i] = kill(A[i], B[i])
        if 0 < B[i]:
            A[i+1], B[i] = kill(A[i+1], B[i])
    
    return (A, B)

N, A, B = read()
print((sum(A) - sum(main(N, A, B)[0])))
# Original solution
N=int(eval(input()))
A=[int(x) for x in input().rstrip().split()]
B=[int(x) for x in input().rstrip().split()]
ans=0
now=A[0]
for i in range(N):
  if B[i]<=A[i]:
    ans+=B[i]
  else:
    if A[i]+A[i+1]<=B[i]:
      ans+=A[i]+A[i+1]
      A[i+1]=0
    else:
      ans+=B[i]
      A[i+1]=A[i+1]-(B[i]-A[i])
print(ans)



# Optimized solution
n=int(eval(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)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))

num = 0
for i in range(n):
    if a[i] >= b[i]:
       # print(a[i],b[i])
        a[i] -= b[i]
        num += b[i]
        b[i] = 0
        
    elif a[i] < b[i]:
        num += a[i]
        #a[i] = 0
        b[i] -= a[i]
        num += min(b[i],a[i+1])
        a[i+1] = max(a[i+1]-b[i],0)
        
print(num)

# Optimized solution
n = int(eval(input()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
tot = sum(A)
for i in range(n):
    en = A[i] + A[i+1]
    A[i] = max(A[i] -B[i],0)
    if A[i] == 0:
        A[i+1] = max(en - B[i],0)
        #print(A[i],A[i+1])
print((tot-sum(A)))
# Original solution
N = int(eval(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]
        A[i] = 0
        if A[i + 1] <= B[i]:
            ans += A[i + 1]
            B[i] -= A[i + 1]
            A[i + 1] = 0
        else:
            ans += B[i]
            A[i + 1] -= B[i]
    else:
        ans += B[i]
        B[i] = 0
        A[i] -= B[i]

print (ans)

# Optimized solution
N = int(eval(input()))
B = list(map(int, input().split()))
A = list(map(int, input().split()))

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

print (ans)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.append(0)
ans = 0
for i in range(n):
    beat = min(a[i], b[i])
    a[i] -= beat
    b[i] -= beat
    if i != n:
        beat2 = min(a[i + 1], b[i])
        a[i + 1] -= beat2
    ans += beat + beat2
print(ans)


# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.append(0)
ans = 0
for i in range(n):
    beaten = min(a[i], b[i])
    b[i] -= beaten
    beaten2 = min(a[i + 1], b[i])
    a[i + 1] -= beaten2
    ans += beaten + beaten2
print(ans)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.append(0)
ans = 0
for i in range(n):
    beaten = min(a[i], b[i])
    b[i] -= beaten
    beaten2 = min(a[i + 1], b[i])
    a[i + 1] -= beaten2
    ans += beaten + beaten2
print(ans)


# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(n):
    # 正面の町を救う
    beaten = min(a[i], b[i])
    b[i] -= beaten
    # 隣町を救う
    beaten2 = min(a[i + 1], b[i])
    a[i + 1] -= beaten2
    ans += beaten + beaten2
print(ans)
# Original solution
def main():
    N = int(eval(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]:
            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]
            B[i] = 0
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    N = int(eval(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]:
            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)


if __name__ == '__main__':
    main()
# Original solution
def main():
    N = int(eval(input()))
    A = [int(i) for i in input().split()]
    B = [int(i) for i in input().split()]
    ans = 0
    for i in range(N)[::-1]:
        if A[i+1] >= B[i]:
            ans += B[i]
            A[i+1] -= B[i]
            B[i] = 0
        else:
            ans += A[i+1]
            B[i] -= A[i+1]
            A[i+1] = 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)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    N = int(eval(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]
            A[i] -= B[i]
            B[i] = 0
        else:
            ans += A[i]
            B[i] -= A[i]
            if A[i+1] >= B[i]:
                ans += B[i]
                A[i+1] -= B[i]
                B[i] = 0
            else:
                ans += A[i+1]
                B[i] -= A[i+1]
                A[i+1] = 0
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
import sys, heapq, bisect, math, fractions
from collections import deque

N = int(eval(input()))
A = list(map(int, sys.stdin.readline().rsplit()))
B = list(map(int, sys.stdin.readline().rsplit()))

res = 0
for i, b in enumerate(B):
    a = A[i]
    a1 = A[i + 1]
    if a >= b:
        res += b
    else:
        res += a
        if a1 >= b - a:
            A[i + 1] = a1 - (b - a)
            res += b - a
        else:
            A[i + 1] = 0
            res += a1

print(res)

# Optimized solution
import sys

N = int(eval(input()))
A = list(map(int, sys.stdin.readline().rsplit()))
B = list(map(int, sys.stdin.readline().rsplit()))

res = 0
for i, b in enumerate(B):
    a = A[i]
    a1 = A[i + 1]
    if a >= b:
        res += b
    else:
        res += a
        if a1 >= b - a:
            A[i + 1] = a1 - (b - a)
            res += b - a
        else:
            A[i + 1] = 0
            res += a1

print(res)
# Original solution
n=int(eval(input()))
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
ans=0
for i in range(n):
    m=min(a[i],b[i])
    ans+=m
    a[i]-=m
    b[i]-=m
    m=min(a[i+1],b[i])
    a[i+1]-=m
    b[i]-=m
    ans+=m
print(ans)
        


# Optimized solution
from sys import stdin,stdout

if __name__=="__main__":
    n=int(stdin.readline())

    a=[int(x) for x in stdin.readline().split()]
    b=[int(x) for x in stdin.readline().split()]

    cnt=0

    for i in range(n):
        if(a[i]>=b[i]):
            cnt+=b[i]
        else:
            cnt+=a[i]
            d=b[i]-a[i]
            cnt+=min(a[i+1],d)
            if(d>a[i+1]):
                a[i+1]=0
            else: a[i+1]-=d
            
    stdout.write(str(cnt)+"\n")
# Original solution
n = int(eval(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]
  else:
    d = b[i] -a[i]
    if a[i+1] <= d:
      c += a[i] + a[i+1]
      a[i+1] = 0
    else:
      a[i+1] -= d
      c += b[i]
print(c)

# Optimized solution
def main():
  n = int(eval(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]
    else:
      d = b[i] -a[i]
      if a[i+1] <= d:
        c += a[i] + a[i+1]
        a[i+1] = 0
      else:
        a[i+1] -= d
        c += b[i]
  print(c)

if __name__ == '__main__':
  main()
# Original solution
n = int(eval(input()))
enemy = list(map(int, input().split()))
beat = list(map(int, input().split()))
ans = 0 
for i in range(n):
  #左を全力で倒す
  left = min([enemy[i], beat[i]])
  ans += left
  enemy[i] -= left
  beat[i] -= left
  
  right = min([enemy[i+1], beat[i]])
  ans += right
  enemy[i + 1] -= right
  beat[i] -= right
  
print(ans)


# Optimized solution
n = int(eval(input()))
enemy = list(map(int, input().split()))
beat = list(map(int, input().split()))
ans = 0 
for i in range(n):
  #左を全力で倒す
  left = min([enemy[i], beat[i]])
  ans += left
  beat[i] -= left
  
  right = min([enemy[i+1], beat[i]])
  ans += right
  enemy[i + 1] -= right
  beat[i] -= right
  
print(ans)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split())) 
b = list(map(int, input().split())) 
ans = 0

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

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split())) 
b = list(map(int, input().split())) 
ans = 0
 
for i in range(n) :
  if b[i] >= a[i] :
    if a[i+1] >= (b[i]-a[i]) :
      a[i+1] = a[i+1] - (b[i]-a[i])
      ans += b[i]
    else :
      ans += a[i] + a[i+1]
      a[i+1] = 0
  else :
    ans += b[i]
    
print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split())) # N + 1
B = list(map(int, input().split())) # N

# 重みがないのですべて小さい方から倒せると仮定する
cnt = 0
for i in range(N):
    cnt += min(B[i], A[i])
    diff = B[i] - A[i]
    if diff > 0:
        if A[i+1] < diff:
            cnt += A[i+1]
            A[i+1] = 0
        else:
            cnt += diff
            A[i+1] -= diff
        A[i] = 0
    else:
        A[i] -= B[i]
#print("A",A)
#print("B",B)
print(cnt)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split())) # N + 1
B = list(map(int, input().split())) # N

# 重みがないのですべて小さい方から倒せると仮定する
cnt = 0
for i in range(N):
    cnt += min(B[i], A[i])
    diff = B[i] - A[i]
    if diff > 0:
        if A[i+1] < diff:
            cnt += A[i+1]
            A[i+1] = 0
        else:
            cnt += diff
            A[i+1] -= diff
        #A[i] = 0
    #else:
    #    A[i] -= B[i]
#print("A",A)
#print("B",B)
print(cnt)
# Original solution
from collections import defaultdict


if __name__=="__main__":
    brave_n = int(eval(input()))
    monster_dic = defaultdict(int)
    brave_dic = defaultdict(int)
    monster_n = list(map(int, input().split()))
    brave_can = list(map(int, input().split()))
    kill_count = 0

    for i, m in enumerate(monster_n):
        monster_dic[i] = m

    for i, b in enumerate(brave_can):
        brave_dic[i] = b

    for i in range(len(brave_dic)):

        if brave_dic[i] >= monster_dic[i]:
            kill = monster_dic[i]
            monster_dic[i] -= kill
            brave_dic[i] -= kill
            kill_count += kill
            if brave_dic[i] >= monster_dic[i+1]:
                kill = monster_dic[i+1]
                monster_dic[i+1] -= kill
                brave_dic[i] -= kill
                kill_count += kill
            else:
                kill = brave_dic[i]
                monster_dic[i+1] -= kill
                brave_dic[i] -= kill
                kill_count += kill
        else :
            kill = brave_dic[i]
            monster_dic[i] -= kill
            brave_dic[i] -= kill
            kill_count += kill

    print(kill_count)


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
d_sum = sum(A)

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

print((d_sum - sum(A)))
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

temp = 0

for i, j in enumerate(A):
    if i == N:
        break
    if j >= B[i]:
        temp += B[i]
        B[i] = 0
        continue
    else:
        temp += j
        B[i] = B[i] - j

    if A[i + 1] >= B[i]:
        A[i + 1] = A[i + 1] - B[i]
        temp += B[i]
    else:
        temp += A[i + 1]
        A[i + 1] = 0

print(temp)


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0
for i in range(N):
    # i番目の街のモンスターを倒す
    ans += min(A[i], B[i])
    nb = B[i] - min(A[i], B[i])
    # i+1番目の街のモンスターを倒す
    ans += min(A[i+1], nb)
    A[i+1] -= min(A[i+1], nb)
print(ans)