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

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

Solution (Python):
import math
import sys
MAX_INT = int(10e9)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = IL()
b = IL()

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)

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

for i in range(n):
    m = min(a[i], b[i])
    a[i] -= m
    b[i] -= m
    m = min(a[i + 1], b[i])
    a[i + 1] -= m
    b[i] -= m
aft = sum(a)

print(num - aft)

Solution (Python):


from itertools import combinations
from collections import defaultdict
from pprint import pprint
import math
import datetime
import sys

N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
B.append(0)

ans = 0
for i in range(N):
    a = A[i]
    b = B[i]
    en = min(a,b)
    ans += en
    if a < b:
        en = min(A[i+1],(b-a))
        A[i+1] -= en
        ans += en
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):
    m = min(a[i], b[i])
    a[i] -= m
    b[i] -= m
    ans += m
    m = min(a[i + 1], b[i])
    a[i + 1] -= m
    b[i] -= m
    ans += m

print(ans)

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

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

Solution (Python):


def main():
    N = int(input())
    AN = list(map(int, input().split()))
    BN = list(map(int, input().split()))

    res = 0

    for i in range(N - 1, -1, -1):
        res += min(AN[i + 1], BN[i])
        BN[i] = max(0, BN[i] - AN[i + 1])

        res += min(BN[i], AN[i])
        AN[i] = max(0, AN[i] - BN[i])

    print(res)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
city = [int(x) for x in input().split()]
hero = [int(y) for y in input().split()]
monster = 0  
for i in range(N):
    if hero[i] > city[i]:
        hero[i] -= city[i]
        monster += city[i]
        city[i] = 0
        if hero[i] > city[i + 1]:
            monster += city[i+1]
            city[i + 1] = 0
        else:
            city[i + 1] -= hero[i]
            monster += hero[i]
    else:
        monster += hero[i]


print(monster)

Solution (Python):
N = int(input())
A_list = [int(x) for x in input().split()]
B_list = [int(x) for x in input().split()]
cnt = 0
for i, B in enumerate(B_list):
    A = A_list[i]
    cnt += min(A, B)
    if (B > A):
        diff = min(A_list[i+1], B-A)
        A_list[i+1] -= diff
        cnt += diff
print(cnt)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
extra=0
sum=0
for i in range(len(B)):
  if B[i]+extra>=A[i]:
    sum+=A[i]
    if (A[i]-extra)>=0:
    	extra=B[i]-(A[i]-extra)
    else:
      extra=B[i]
  else:
    sum+=(B[i]+extra)
    extra=0
if A[-1]>=extra:
	print(sum+extra)
else:
  print(sum+A[-1])

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]:
            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
r = a[0]
for i in range(n):
    if b[i] <= r:
        ans += b[i]
        r = a[i + 1]
    else:
        p = b[i] - r
        if p < a[i + 1]:
            ans += b[i]
            r = a[i + 1] - p
        else:
            ans += r + a[i + 1]
            r = 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]
    else:
        ans += A[i]
        B2 = B[i] - A[i]
        if A[i+1] >= B2:
            ans += B2
            A[i+1] -= B2
        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()))

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

print(count)

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = [0] * (N+3)
B.append(0)
B.append(0)
total = 0
for i in range(N+1):
    if(A[i] <= B[i] + C[i]):
        total += A[i]
        if(A[i] <= C[i]):
            C[i+1] = B[i]
        else:
            C[i+1] = B[i] + C[i] - A[i]
    else:
        total += B[i] + C[i]



print(total)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, d=DVSR): return pow(x, d - 2, d)
def DIV(x, y, d=DVSR): return (x * INV(y, d)) % d
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
N=II()
AS=LI()
BS=LI()

sm = 0
for i in range(N):
    rest = BS[i]
    taosu = min(AS[i], rest)
    AS[i] -= taosu
    rest -= taosu
    sm += taosu

    taosu2 = min(AS[i+1], rest)
    AS[i+1] -= taosu2
    rest -= taosu2
    sm += taosu2

print(sm)

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

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

print(score)

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

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

print(z)

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

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

  if limit > A[i+1]:
    answer += A[i+1]
    limit -= A[i+1]
    A[i+1] = 0
  else:
    A[i+1] -= limit
    answer += limit
    limit = 0

print(answer)

Solution (Python):

"""
AtCoder A
"""

import sys, math, random
import numpy as np








inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    N=I()
    A = list(map(int,input().split()))
    B=LI()
    bef=0
    out=0
    for i in range(N):
        if A[i]>bef:
            en=A[i]-bef
            out+=bef
            if en>B[i]:
                bef=0
                out+=B[i]
            else:
                bef=B[i]-en
                out+=en
        else:
            out+=A[i]
            bef=B[i]
    i=N
    if A[N]>bef:
        en=A[N]-bef
        out+=bef
    else:
        out+=A[N]
        

    print(out)

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

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

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

print(mon)

Solution (Python):
import sys
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
from collections import deque  
from collections import defaultdict  
from heapq import heapify, heappush, heappop, heappushpop  
from bisect import bisect_left, bisect_right  
sys.setrecursionlimit(int(1e+6))
MOD = int(1e+9) + 7



def main():
    N = i2n()
    A = i2nn()
    B = i2nn()
    n = 0   
    for i in range(N):
        t = min(A[i], B[i])
        n += t
        A[i] -= t
        B[i] -= t
        t = min(A[i+1], B[i])
        n += t
        A[i+1] -= t
        B[i] -= t
    print(n)
    
    

main()

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main(): 
    N = II()
    A = LI()
    B = LI()

    A_sum = 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(A_sum - sum(A))


main()

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

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

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>0 and B[i-1]>0:
		if A[i] >= B[i-1]:
			ans += B[i-1]
			A[i]-=B[i-1]
		else:
			ans += A[i]
			A[i]=0
	if A[i]>=B[i]:
		ans+=B[i]
		B[i]=0
	else:
		B[i]-=A[i]
		ans+=A[i]
if A[-1]>0:
	ans+=min(A[-1],B[-1])
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(n):
    kill = min(a[i],b[i])
    res += kill
    a[i] -= kill
    b[i] -= kill
    
    kill = min(a[i+1],b[i])
    res += kill
    a[i+1] -= kill
    b[i] -= kill
print(res)

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

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

s = 0
for i in range(N):
    if Bs[i] > As[i]:
        s += As[i]
        Bs[i] -= As[i]
    else:
        s += Bs[i]
        Bs[i] = 0
    if Bs[i] > As[i + 1]:
        s += As[i + 1]
        As[i + 1] = 0
    else:
        s += Bs[i]
        As[i + 1] -= Bs[i]
print(s)

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

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

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

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

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

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

Monst = 0
killed = 0
for n,b in enumerate(B):
        cur = A[n]
        nex = A[n+1]
        cur -= killed
        if b<=cur:
            Monst += b
            killed = 0
        elif b<=cur+nex:
            Monst += b
            killed = b - cur
        else:
            Monst += cur+nex
            killed = nex
print(Monst)

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

cnt = 0
res = 0
for i in range(N):
    enm = min(A[i]+A[i+1]-res, B[i])
    res = max(res + enm - A[i], 0)
    cnt += enm

print(cnt)

Solution (Python):
n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))

ans=0
for i in range(n-1,-1,-1):
	val=min(arr2[i],arr1[i+1])
	ans+=val
	arr2[i]-=val
	arr1[i+1]-=val
	val=min(arr2[i],arr1[i])
	ans+=val
	arr2[i]-=val
	arr1[i]-=val
	
print(ans)

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

cnt=0
for n in range(N):
    mons = min(A[n],B[n])
    A[n] -= mons
    B[n] -= mons
    cnt += mons
    if B[n]>0:
        mons = min(A[n+1], B[n])
        A[n+1] -= mons
        cnt += mons

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]
    else:
        cnt += A[i]
        if A[i+1] > B[i]-A[i]:
            cnt += B[i]-A[i]
            A[i+1] -= B[i]-A[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()))
ans = 0
for k in range(N):
    if A[k] <= B[k]:
        ans += A[k]
        d = B[k]-A[k]
        if d > A[k+1]:
            ans += A[k+1]
            A[k+1] = 0
        else:
            ans += d
            A[k+1] -= d
    else:
        ans += B[k]
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
aBfore = sum(A)
for i in range(N):
    if A[i] <= B[i]:
        B[i] -= A[i]
        A[i] = 0
        A[i+1] = max(A[i+1] - B[i], 0)
    else:
        A[i] -= B[i]
aAfter = sum(A)
print(aBfore - aAfter)

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

ans = 0

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

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

print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right; from collections import Counter, defaultdict, deque, OrderedDict; from copy import deepcopy; from fractions import gcd; from functools import lru_cache, reduce; from math import ceil, floor; from sys import stdin, setrecursionlimit
import heapq as hq; import itertools as its; import operator as op



inf = float('inf')
input = stdin.readline


def main():
    n = get_int()
    A = get_li()
    B = get_li()
    ans = 0
    left = 0
    for i, a in reversed(list(enumerate(A))):
        if i == 0:
            ans += min(left, A[0])
            break
        a_ = max(0, a - left)
        ans += a - a_
        left = max(0, B[i-1] - a_)
        ans += B[i-1] - left
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def gets(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input().strip()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().strip().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    setrecursionlimit(4100000)
    main()

Solution (Python):
from copy import *
from sys import *
import math
import queue
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)

def main():
	n = int(input())
	a = list(map(int,input().split()))
	b = list(map(int,input().split()))
	t = sum(b)
	for i in reversed(range(n)):
		c = a[i+1] - b[i]
		a[i+1] = 0 if c<0 else c
		b[i] = 0 - c if c < 0 else 0
		c = a[i] - b[i]
		a[i] = 0 if c<0 else c
		b[i] = 0 - c if c < 0 else 0
	print(t-sum(b))

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(m,n):
	x = max(m,n)
	y = min(m,n)
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	return y

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    a,b = LI()
    if (a-b)%2 == 0:
        print((a+b)//2)
    else:
        print("IMPOSSIBLE")
    return


def B():
    n = I()
    p = LI()
    k = [p[i] for i in range(n)]
    k.sort()
    s = 0
    for i in range(n):
        s += (p[i] != k[i])
    if s < 3:
        print("YES")
    else:
        print("NO")
    return


def C():
    n = I()
    a = LI()
    b = LI()
    b.append(0)
    ans = 0
    sb = sum(b)
    i = n
    while i >= 0:
        if a[i] > b[i]:
            a[i] -= b[i]
            b[i] = 0
        else:
            b[i] -= a[i]
            a[i] = 0
        if a[i] > b[i-1]:
            a[i] -= b[i-1]
            b[i-1] = 0
        else:
            b[i-1] -= a[i]
            a[i] = 0
        i -= 1

    print(sb-sum(b))
    return


def D():
    n = I()

    return


def E():
    n = I()

    return


def F():
    n = I()

    return


if __name__ == "__main__":
    C()

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

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline

n=int(input())
aa=list(map(int,input().split()))
bb=list(map(int,input().split()))

ans=0
for i,b in enumerate(bb):
    if b<=aa[i]:
        ans+=b
    else:
        ans+=aa[i]
        b-=aa[i]
        if b<=aa[i+1]:
            aa[i+1]-=b
            ans+=b
        else:
            ans+=aa[i+1]
            aa[i+1]=0
print(ans)

Solution (Python):
n = int(input())

a = list(map(int, input().split()))
b = list(map(int, input().split())) + [0]
c = [0] * (n + 1)

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

print(cnt)

Solution (Python):


def main():
    n = int(input())
    A = list(map(int, input().rstrip().split()))
    B = list(map(int, input().rstrip().split()))
    ans = 0
    for i in range(n):
        cur1 = min(A[i], B[i])
        cur2 = min(A[i + 1], B[i] - cur1)
        A[i] -= cur1
        A[i + 1] -= cur2
        ans += cur1 + cur2
    print(ans)


if __name__ == "__main__":
    main()

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

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

ans = 0
tmp = 0
for i in range(N+1):
    if i == N:
        if A[i] <= tmp:
            ans += A[i]
        else:
            ans += tmp
    else:
        if A[i] >= B[i] + tmp:
            ans += B[i] + tmp
            tmp = 0
        else:
            if A[i] <= tmp:
                ans += A[i]
                tmp = B[i]
            else:
                ans += A[i]
                tmp = B[i] + tmp - A[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):
    ans += min(a[i] + a[i + 1], b[i])
    r = max(0, b[i] - a[i])
    a[i + 1] = max(0, a[i + 1] - r)
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):
    if a[i] >= b[i]:
        count += b[i]
    else:
        count += a[i]
        b[i] -= a[i]
        if a[i+1] >= b[i]:
            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()))
C = B[:]
output = 0

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

for i in range(N):
    output += B[i]-C[i]

print(output)

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

ans = 0
pre = 0
for a, b in zip(A, B):
    if a >= pre:
        a -= pre
        ans += pre
    else:
        ans += a
        pre = b
        continue
    if a >= b:
        ans += b
        pre = 0
    else:
        ans += a
        pre = b - a
a = A[-1]
if a >= pre:
    ans += pre
else:
    ans += a
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])
    ans += a
    B[i] -= a
    b = min(A[i+1], B[i])
    ans += b
    A[i+1] -= b
print(ans)

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

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

print(count)

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

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

print(num -sum(a))

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n = ri()
    la = rli()
    lb = rli()
    ans = 0
    for i in range(len(lb)):
        b = lb[i]
        a0 = la[i]
        a1 = la[i+1]
        d0 = min(b, a0)
        b -= d0
        d1 = min(b, a1)
        la[i+1] = a1-d1
        ans += d0 + d1
    print(ans)


if __name__ == '__main__':
    main()

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

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

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
from functools import reduce
import sys
import math
import bisect
import random
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return list(map(float, sys.stdin.readline().split()))
def LS():return sys.stdin.readline().split()
def SR(n): return [list(sys.stdin.readline())[:-1] for i in range(n)]
def IR(n): return [int(sys.stdin.readline()) for i in range(n)]
def LIR(n): return [list(map(int, sys.stdin.readline().split())) for i in range(n)]
def LSR(n): return [list(map(list, sys.stdin.readline().split())) for i in range(n)]
_gcd = lambda x, y: _gcd(y, x%y) if (x%y) else y
_lcm = lambda x, y: x*y // _gcd(x, y)
def gcd(*numbers):
    return reduce(_gcd, numbers)
def lcm(*numbers):
    return reduce(_lcm, numbers)
sys.setrecursionlimit(1000000)
INF = float('inf')
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

N = I()
A = LI()
B = LI()
ans = 0
left = 0
for i in range(N):
    if (A[i]<=B[i]+left): 
        ans += A[i]
        if (A[i]>=left): 
            left = B[i] -(A[i]-left)
        else:
            left = B[i]
    else:
        ans += B[i]+left
        left = 0
ans += min(left, A[-1])
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]:
        t=b[i]
        a[i] -= t
        b[i] -= t
        ans += t
    else:
        t=a[i]
        a[i] -= t
        b[i] -= t
        ans += t

    if a[i + 1] >= b[i]:
        t=b[i]
        a[i+1]-=t
        b[i] -= t
        ans += t
    else:
        t = a[i + 1]
        a[i+1]-=t
        b[i] -= t
        ans+=t
print(ans)

Solution (Python):
def get_ints():
    return list(map(int, input().split()))

n = int(input())
a = get_ints()
b = get_ints()

total = 0

for i in range(n):
    l = min(b[i], a[i]);
    total += l;
    if b[i] > l:
        r = min(b[i] - l, a[i + 1])
        total += r
        a[i + 1] -= r

print(total)