CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))
a_li = [int(i) for i in input().split()]
b_li = [int(i) for i in input().split()]

if b_li[0]<b_li[-1]:
    a_li = a_li[::-1]
    b_li = b_li[::-1]

ans = 0
for i in range(0,n):
    attack = min(a_li[i],b_li[i])
    attack2 = min(a_li[i+1],b_li[i]-min(a_li[i],b_li[i]))
    a_li[i]-=attack
    a_li[i+1]-=attack2
    b_li[i]-=(attack+attack2)
    ans+=(attack+attack2)

print(ans)



# Optimized solution
n = int(eval(input()))
a_input = list(map(int,input().split()))
b_input = list(map(int,input().split()))

ans=0
for i in range(n):
    first_attack=0
    second_attack=0

    first_attack = min(b_input[i],a_input[i])
    a_input[i]-=first_attack
    if first_attack<b_input[i]:
        second_attack = min(a_input[i+1],b_input[i]-first_attack)
        a_input[i+1]-= second_attack
    ans+=first_attack+second_attack

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

sum_monster = 0
add = 0

def trp(l, n):
    return l[:n] + [0]*(n-len(l))
B = trp(B, len(A))

for (mons, hero) in zip(A, B):
  hero_added = hero + add
  if mons >= hero_added:
    sum_monster += hero_added
    add = 0
  else:
    sum_monster += mons
    if (hero_added - mons) >= hero:
      add = hero
    else:
      add = (hero_added - mons)
print(sum_monster)

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

ko = 0

for i in range(N):
  if A[i] + A[i+1] > B[i]:
    ko += B[i]
    if A[i] < B[i]:
      A[i+1] -= (B[i] - A[i])
  else:
    ko += A[i] + A[i+1]
    A[i+1] = 0
    
print(ko)
# Original solution
a = int(eval(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)


# Optimized solution
a = int(eval(input()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
e = 0
for i in range(a):
    d = min(c[i], b[i])
    e += d
    c[i] -= d
    b[i] -= d
    d = min(c[i], b[i+1])
    e += d
    c[i] -= d
    b[i+1] -= d
print(e)
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(eval(input()))
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
before = sum(A)

for i in range(N):
    nokori = B[i]
    for j in range(i,i+2):
        if nokori >= A[j]:
            nokori -= A[j]
            A[j] = 0
        else:
            A[j] -= nokori
            nokori = 0
after = sum(A)

print((before - after))

# Optimized solution
N = int(eval(input()))
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

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

print(cnt)
# Original solution
n=int(eval(input()))
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]

k=0
for i,j in enumerate(b):
  if a[i]+a[i+1]<=j:
    k+=a[i]+a[i+1]
    a[i],a[i+1]=0,0
  elif a[i]<=j:
    k+=j
    a[i],a[i+1]=0,a[i]+a[i+1]-j
  else:
    k+=j
    a[i]-=j
print(k)

# Optimized solution
n=int(eval(input()))
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
ans=0
mon1=a[0]
mon2=a[0]+a[1]
for i in range(n):
   if b[i] <= mon1:
      ans+=b[i]
      mon1=a[i+1]
      if i<=n-2:
         mon2=a[i+1]+a[i+2]
   elif mon1<b[i]<=mon2:
      ans+=b[i]
      mon1=mon2-b[i] #ここmon2-b[i]のほうがきれい？
      if i<=n-2:
         mon2=mon1+a[i+2]
   else: 
      ans+=mon2 #ここmon2にしないと動かないぞ
      mon1=0
      if i<=n-2:
         mon2=a[i+2]
print(ans)
# 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)):
    c_i = 0
    c_i = min(a[i], b[i])
    c += c_i
    b[i] -= c_i
    c_i_1 = 0
    c_i_1 = min(a[i+1], b[i])
    c += c_i_1
    a[i+1] -= c_i_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(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)
# Original solution
n=int(eval(input()))
ai=[]
for i in map(int,input().split()):
    ai.append(i)
bi=[]
for i in map(int,input().split()):
    bi.append(i)

asum=0
for i in range(n+1):
    asum=asum+ai[i]    

i=0
if ai[i]<=bi[i]:
    bi[i]=bi[i]-ai[i]
    ai[i]=0
else :
    ai[i]=ai[i]-bi[i]
    bi[i]=0
for i in range(1,n,1):
    if ai[i]<=bi[i-1]:
        bi[i-1]=bi[i-1]-ai[i]
        ai[i]=0
    else :
        ai[i]=ai[i]-bi[i-1]
        bi[i-1]=0
    if ai[i]<=bi[i]:
        bi[i]=bi[i]-ai[i]
        ai[i]=0
    else :
        ai[i]=ai[i]-bi[i]
        bi[i]=0
i=n
if ai[i]<=bi[i-1]:
    bi[i-1]=bi[i-1]-ai[i]
    ai[i]=0
else :
    ai[i]=ai[i]-bi[i-1]
    bi[i-1]=0

asum2=0
for i in range(n+1):
    asum2=asum2+ai[i]    

print((asum-asum2))        

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

fii=min(a[0],b[0])
fij=min(a[1],b[0]-fii)
icnt=fii+fij
for i in range(1,n):
    fii=min(a[i]-fij,b[i])
    fij=min(a[i+1],b[i]-fii)
    icnt+=fij+fii

print(icnt)
# Original solution
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
B = lr()
answer = 0
for i in range(N):
    a, b = A[i], B[i]
    answer += min(a, b)
    if a < b:
        remain = b - a
        add = min(A[i+1], remain)
        answer += add
        A[i+1] -= add

print(answer)


# Optimized solution
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
B = lr()
remain = 0
answer = 0
for i in range(N):
    attack = min(A[i], remain)
    answer += attack
    A[i] -= attack
    if A[i] > 0:
        attack = min(A[i], B[i])
        answer += attack
        B[i] -= attack
    remain = B[i]

answer += min(A[N], remain)
print(answer)
# Original solution
N = int(eval(input()))
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
killed = 0
rest = 0
for i in range(0, N):
  killed = min(H[i]+rest, M[i])
  rest = min(H[i]+rest-killed, H[i])
  sum_m += killed
sum_m += min(rest, M[-1])
print(sum_m)


# Optimized solution
N = int(eval(input()))
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
rest = 0
for i in range(N):
  if M[i] >= H[i]+rest:
    sum_m += H[i]+rest
    rest = 0
  else:
    sum_m += M[i]
    rest = min(H[i], H[i]+rest-M[i])
sum_m += min(rest, M[-1])

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

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

# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
tot = 0
c = 0
for i in range(N):
    a = A[i]
    b = B[i]
    if a>=c:
        a -= c
        tot += c
        if a>=b:
            tot += b
            c = 0
        else:
            tot += a
            c = b-a
    else:
        tot += a
        c = b
a = A[N]
tot += min(a,c)
print(tot)
# Original solution
# 初案のアルゴリズムが違っていた。難しかった。

n=int(eval(input()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
b.append(0) #ダミー
z = 0
c = b[0]
for i in range(n+1):
    x = a[i] - c
    if i < n:
        if x <= 0:
            z += a[i]
            if -x >= a[i+1]:
                c = b[i+1] + a[i+1]
            elif -x < a[i+1]:
                c = b[i+1] - x
        elif x > 0:
            z += c
            c = b[i+1]
    elif i == n:
        if x <= 0:
            z += a[i]
        else:
            z += c
print(z)


# Optimized solution
n = int(eval(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)
# Original solution
def LI():
  return [int(s) for s in input().split()]

N = int(eval(input()))

A_s = LI()
B_s = LI()

counter = 0

for i in range(N):
  if A_s[i] > B_s[i]:
    counter+=B_s[i]
  elif B_s[i] < A_s[i] + A_s[i+1]:
    counter+=B_s[i]
    A_s[i+1] = A_s[i+1] - (B_s[i]-A_s[i])
  else:
    counter+=A_s[i]+A_s[i+1]
    A_s[i+1] = 0
    
print(counter)

# Optimized solution
N = int(eval(input()))
A = [int(s) for s in input().split()]
sum_A = sum(A)
B = [int(s) for s in input().split()]
for i in range(N):
    attack1 = A[i] - B[i]
    if attack1 < 0:
        attack2 = A[i + 1] + attack1
        A[i] = 0
        A[i + 1] = attack2 if attack2 >= 0 else 0
    else:
        A[i] = attack1

print((sum_A - sum(A)))
# Original solution
N = int(eval(input()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
start = [0]*(N+1)
attack = 0
enemy = 0

for i in range(N+1):
    start[i] = A[i]

for i in range(N):
    attack = min(A[i],B[i])
    A[i] = A[i] - attack
    B[i] = B[i] - attack
    attack = min(A[i+1],B[i])
    A[i+1] = A[i+1] - attack
    B[i] = B[i] - attack
    
for i in range(N+1):
    enemy = enemy + (start[i]-A[i])
    
print(enemy)

# 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):
    tmp = 0
    if A[i] >= B[i]:
        A[i] -= B[i]
        ans += B[i]
    else:
        ans += A[i]
        tmp = B[i] - A[i]
        A[i] = 0
        if A[i+1] >= tmp:
            A[i+1] -= tmp
            ans += tmp
        else:
            ans += A[i+1]
            A[i+1] = 0
            
print(ans)
# Original solution
#C
n = int(eval(input()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
max_num = 0

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

# 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] >= 0:
        a[i] -= b[i]
        ans += b[i]
        b[i]=0
    else:
        ans += a[i]
        b[i] -= a[i]
        a[i] = 0
        if a[i+1] - b[i] >= 0:
            ans += b[i]
            a[i+1] -= b[i]
            b[i] = 0
        else:
            ans += a[i+1]
            a[i+1] = 0
        
print(ans)
# Original solution
N = int(eval(input()))
m = list(map(int, input().split()))
b = list(map(int, input().split()))
s = 0
for i in range(N):
  l = min(m[i],b[i])
  s += l
  m[i] -= l
  b[i] -= l
  
  r = min(m[i+1],b[i])
  s += r
  m[i+1] -= r
print(s)

# Optimized 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+=int(b[i])
  else:
    if (a[i]+a[i+1])>=b[i]:
      c+=int(b[i])
      a[i+1]=a[i]+a[i+1]-b[i]
    else:
      c+=int(a[i]+a[i+1])
      a[i+1]=0
print(c)
# 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):
    ans += min(A[i], B[i])
    B[i] -= min(A[i], B[i])
    ans += min(A[i+1], B[i])
    A[i+1] -= min(A[i+1], B[i])
print(ans)

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

ans = sum(A)

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

print((ans - sum(A)))
# Original solution
inf = 10 ** 18
P = 10 ** 9 + 7

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

A_all = sum(A)

xi = min(A[0], B[0])
A[0] -= xi
B[0] -= xi
for i in range(N):
    yi = min(B[i], A[i + 1])
    A[1 + i] -= yi
    B[i] -= yi
    #
    if i < N - 1:
        xi = min(A[1 + i], B[1 + i])
        A[1 + i] -= xi
        B[1 + i] -= xi
    #
    # print("#", A, B, A_all - sum(A))
    
A_r = list(reversed(A))
B_r = list(reversed(B))


xi = min(A_r[0], B_r[0])
A_r[0] -= xi
B_r[0] -= xi
for i in range(N):
    yi = min(B_r[i], A_r[i + 1])
    A_r[1 + i] -= yi
    B_r[i] -= yi
    #
    if i < N - 1:
        xi = min(A_r[1 + i], B_r[1 + i])
        A_r[1 + i] -= xi
        B_r[1 + i] -= xi
    #
    # print("#", A, B, A_all - sum(A))

print((A_all - sum(A)))

# 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):
    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)
# Original solution
n,*a=list(map(int,open(0).read().split()))
c=0
for i in range(1,n+1):c+=min(a[i-1]+a[i],a[n+i]);a[i]-=max(min(a[n+i]-a[i-1],a[i]),0)
print(c)

# Optimized solution
n,x,*a=list(map(int,open(0).read().split()))
c=0
for a,b in zip(a,a[n:]):c+=min(x+a,b);x=a-max(min(b-x,a),0)
print(c)
# Original solution
n=int(eval(input()))
a=list(map(int, input().split()))
b=list(map(int, input().split()))

knock=0
ex=0
for i in range(n):
    if a[i] < b[i]:
        ex=b[i]-a[i]
        knock+=a[i]
    elif a[i] >= b[i]:
        ex=0
        knock+=b[i]

    if ex != 0:
        if a[i+1] - ex <= 0:
            knock+=a[i+1]
            a[i+1]=0
        else :
            knock+=ex
            a[i+1]=a[i+1]-ex

print(knock)


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

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

    if ex != 0:
        if a[i+1] - ex <= 0:
            knock += a[i+1]
            a[i+1] = 0
        else:
            knock += ex
            a[i+1] -= ex

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

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

    if ex != 0:
        if a[i+1] - ex <= 0:
            knock += a[i+1]
            a[i+1] = 0
        else:
            knock += ex
            a[i+1] -= ex

print(knock)


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

knock = 0
ex = 0
for i in range(n):
    if a[i] < b[i]:
        ex = b[i] - a[i]
        knock += a[i]
    elif a[i] >= b[i]:
        ex = 0
        knock += b[i]

    if ex != 0:
        if a[i+1] < ex:
            knock += a[i+1]
            a[i+1] = 0
        else:
            a[i+1] -= ex
            knock += ex

print(knock)
# Original solution
N = int(eval(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)

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

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

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

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

ans = 0
for i,b in enumerate(B):
    x = min(A[i], b)
    ans += x
    b -= x
    y = min(A[i+1], b)
    ans += y
    A[i+1] -= y
print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans, surplus = 0, 0
for i in range(N):
    tmp = min(A[i], surplus)
    ans += tmp
    A[i] -= tmp
    if B[i] > A[i]:
        ans += A[i]
        surplus = B[i] - A[i]
    else:
        ans += B[i]
        surplus = 0
tmp = min(A[N], surplus)
ans += tmp
print(ans)


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

ans, mod = 0, 0

for i in range(N):

    if mod > A[i]:
        ans += A[i]
        A[i] = 0
    else:
        ans += mod
        A[i] -= mod
        mod = 0

    if B[i] > A[i]:
        ans += A[i]
        mod = B[i] - A[i]
    else:
        ans += B[i]
        mod = 0

ans += min(mod, A[N])

print(ans)
# Original solution
import math

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

print(ans)
# Original solution
import sys
def main():
  n, *lst = list(map(int, sys.stdin.read().split()))
  a = lst[n::-1]
  b = lst[-1:n:-1]
  res = 0
  for i in range(n):
    if i == 0:
      tmp = min(b[i], a[i])
      res += tmp
      a[i] -= tmp
      b[i] -= tmp
    else:
      tmp = min(b[i] + b[i - 1], a[i])
      res += tmp
      a[i] -= tmp
      b[i] -= min(b[i], max(tmp - b[i-1], 0))
  res += min(b[n - 1], a[n - 1] + a[n])
  print(res)


if __name__ == '__main__':
  main()

# Optimized solution
import sys
def main():
  n, *lst = list(map(int, sys.stdin.read().split()))
  a = lst[:n+1]
  b = lst[n+1:]
  res = 0
  for i in range(n):
    if i == 0:
      tmp = min(a[i], b[i])
      res += tmp
      b[i] -= tmp
    else:
      tmp = min(a[i], b[i] + b[i - 1])
      res += tmp
      b[i] -= min(b[i], max(tmp - b[i - 1], 0))
  res += min(a[n], b[n - 1])
  print(res)


if __name__ == '__main__':
  main()
# Original solution
from collections import deque
from sys import stdin


def main():
    lines = stdin.readlines()
    A = deque(list(map(int, lines[1].split())))
    B = deque(list(map(int, lines[2].split())))
    cnt = 0
    ans = 0
    while B:
        a = A.pop()
        b = B.pop()
        tmp = a if a < b else b
        ans += tmp
        if cnt % 2:
            A.append(a - tmp)
        else:
            B.append(b - tmp)
        cnt += 1
    print(ans)
    return


main()


# Optimized solution
from sys import stdin


def main():
    lines = stdin.readlines()
    A = tuple(map(int, lines[1].split()))
    B = tuple(map(int, lines[2].split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()
# Original solution
from sys import stdin

readline = stdin.readline


def main():
    readline()
    A = tuple(map(int, readline().split()))
    B = tuple(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()


# Optimized solution
from sys import stdin

readline = stdin.readline


def main():
    eval(input())
    A = tuple(map(int, readline().split()))
    B = tuple(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()
# Original solution
from sys import stdin


def main():
    eval(input())
    readline = stdin.readline
    A = tuple(map(int, readline().split()))
    B = tuple(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()


# Optimized solution
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    A = tuple(map(int, readline().split()))
    B = tuple(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()
# Original solution
from sys import stdin

readline = stdin.readline


def read():
    return list(map(int, readline().split()))


def main():
    eval(input())
    *A, = read()
    *B, = read()
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()


# Optimized solution
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    *A, = list(map(int, readline().split()))
    *B, = list(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()
# Original solution
N = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.reverse()
b.reverse()
ans = 0
capacity = 0
for i in range(N):
    ans += min(b[i] + capacity, a[i])
    capacity = max(b[i] - max(a[i] - capacity, 0), 0)
ans += min(a[-1], capacity)
print(ans)

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

a = a[::-1]
b = b[::-1]
ans = 0
for i, bi in enumerate(b):
    ans += min(a[i], b[i])
    remain = max(0, b[i] - a[i])
    ans += min(a[i + 1], remain)
    a[i + 1] = max(0, a[i + 1] - remain)
print(ans)