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-----
2
3 5 2
4 5

-----Sample Output-----
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.

Difficulty: introductory

Starter code:

Solutions:
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 10 11:20:10 2020

@author: liang
"""

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

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

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = [0]*(N+1) # 残り
cnt = 0
C[0] = A[0]
for i in range(1, N+1):
  #i-1番目の勇者が攻撃した後、i番目の街に残っているモンスターはC[i-1]体。N+1番目の街ではC[N]
  #i番目の勇者B[i-1]はi番目の街A[i-1]を攻撃したあと余力があれば(i+1)番目A[i]まで攻撃できる。
  
  if C[i-1] > B[i-1]: #余力がない
    C[i] = A[i]
    cnt += A[i-1] + B[i-1] -C[i-1] #i番目の街で倒したモンスター
    
  elif C[i-1] <= B[i-1]:
    if A[i] -B[i-1] + C[i-1] >=0:
      C[i] = A[i] -B[i-1] + C[i-1]
      cnt += A[i-1]
      
    else: #余力が有り余っている場合
      C[i] = 0
      cnt += A[i-1]
      


if B[N-1]-C[N-1] > 0:
  ans = cnt + min(A[N], (B[N-1] - C[N-1]))
else:
  ans = cnt
print(ans)

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

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


N = int(input())

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

ans = 0
pool = 0
target = 0
for i in range(len(A)):
    if i == len(A)-1:
        if pool >= A[i]:
            ans += A[i]
        else:
            ans += pool
        break

    if pool >= A[i]:
        ans += A[i]
        target = 0
    else:
        ans += pool
        target = A[i] - pool
    ans += min(target,B[i])
    pool = max(0,B[i] - target)


print(ans)


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


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

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

cnt = 0
for i in range(n):
    d = min(A[i], B[i])
    cnt += d
    A[i] -= d
    B[i] -= d
    d = min(A[i + 1], B[i])
    cnt += d
    A[i+1] -=d

print(cnt)

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)

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

cnt = 0

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

print(cnt)

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

ans = 0
for i in range(N+1):
  taosukazu1 = min(A[i],B[i])
  B[i] -= taosukazu1
  A[i] -= taosukazu1
  ans += taosukazu1
  if B[i] > 0:
    taosukazu2 = min(A[i+1],B[i])
    B[i] -= taosukazu2
    A[i+1] -= taosukazu2
    ans += taosukazu2
print(ans)

n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
B.append(0)

for i in reversed(list(range(n))):
    if B[i] <= A[i]+A[i+1]:
        ans += B[i]
        if B[i] < A[i+1]:
            A[i] = A[i]
        else:
            A[i] = A[i]-(B[i]-A[i+1])
    else:
        ans += A[i]+A[i+1]
        A[i] = 0
print(ans)


from numpy import cumsum
n = int(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] -= max(0, min(b[i] - a[i], a[i+1]))

print(ans)


import copy

N = int(input())
A = list(map(int, input().split()))
A_copy = copy.deepcopy(A)
B = list(map(int, input().split()))

ans = 0

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

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

N = int(input())
As = input().split(' ')
As = [int(As[i]) for i in range(N+1)]
Bs = input().split(' ')
Bs = [int(Bs[i]) for i in range(N)]
WIN = 0
for i in range(N):
  if Bs[i] > As[i]:
    WIN += As[i]
    Bs[i] -= As[i]
    As[i] = 0
    if Bs[i] > As[i+1]:
      WIN += As[i+1]
      Bs[i] -= As[i+1]
      As[i+1] = 0
    elif Bs[i] == As[i+1]:
      WIN += As[i+1]
      Bs[i] = 0
      As[i+1] = 0
    else:
      WIN += Bs[i]
      As[i+1] -= Bs[i]
      Bs[i] = 0
  elif Bs[i] == As[i]:
    WIN += As[i]
    Bs[i] = 0
    As[i] = 0
  else:
    WIN += Bs[i]
    As[i] -= Bs[i]
    Bs[i] = 0
print(WIN)

n=int(input());a,b,c=[*map(int,input().split())],[*map(int,input().split())],0
for i in range(n):
  d=min(a[i],b[i])
  c+=d
  a[i]-=d
  b[i]-=d
  d=min(a[i+1],b[i])
  c+=d
  a[i+1]-=d
print(c)

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

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

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

print((x - sum(a)))


N = int(input())
M_List = list(map(int,input().split()))
N_List = list(map(int,input().split()))
ans = 0
for i in range(N):
    if N_List[i] >= M_List[i]:
        ans += M_List[i]
        ans += (M_List[i+1],N_List[i]-M_List[i])[N_List[i]-M_List[i]<=M_List[i+1]]
        M_List[i+1] = max(0,M_List[i+1]-(N_List[i]-M_List[i]))
    else:
        ans += N_List[i]
print(ans)

N = int(input())
A_ls = list(map(int, input().split(' ')))
B_ls = list(map(int, input().split(' ')))
rst = 0

for i, val in enumerate(B_ls):
    if A_ls[i] + A_ls[i + 1] >= val:
        rst += val
        if A_ls[i] >= val:
            A_ls[i] -= val
        else:
            A_ls[i + 1] -= (val - A_ls[i])
    else:
        rst += (A_ls[i] + A_ls[i + 1])
        A_ls[i], A_ls[i + 1] = 0, 0
print(rst)

# coding: utf-8


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

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

    print(ans)

def __starting_point():
    main()

__starting_point()

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

c=0

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

print(c)

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

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

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

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

n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
num = 0
for i in range(n):
    min_ab = min(a[i],b[i])
    num += min_ab
    a[i] -= min_ab
    b[i] -= min_ab
    if b[i] != 0:
        min_ab = min(a[i + 1],b[i])
        num += min_ab
        a[i + 1] -= min_ab
        b[i] -= min_ab
print(num)

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

N = int(input())
An = list(map(int,input().split()))
Bn = list(map(int,input().split()))
sum = 0
for i in range(N):
  if Bn[i] <= An[i]:
    sum += Bn[i]
  elif Bn[i] <= An[i] + An[i + 1]:
    sum += Bn[i]
    An[i + 1] += An[i] - Bn[i]
  else:
    sum += An[i + 1] + An[i]
    An[i + 1] = 0
print(sum)

N = int(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])
    if A[i] < B[i]:
        if A[i+1] < B[i] - A[i]:
            cnt += A[i+1]
            A[i+1] = 0
        else:
            cnt += B[i] - A[i]
            A[i+1] -= B[i] - A[i]
print(cnt)




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

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

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

N = int(input())
list_a = list(map(int,input().split()))
list_b = list(map(int,input().split()))
c = sum(list_a)

for i in range(1,len(list_a)):
    temp = list_a[i] - min((list_a[i-1] - list_b[i-1]),0)*(-1)
    if list_a[i-1] - list_b[i-1] <= 0:
        list_a[i-1] = 0
    else:
        list_a[i-1] -= list_b[i-1]
    if temp != list_a[i] and temp >= 0:
        list_a[i] = temp
    elif temp <= 0:
        list_a[i] = 0

del_c = sum(list_a)

ans = c - del_c
print(ans)

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

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

print(res)

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

cnt=0
for i in range(n):
  c=b[i]-a[i]
  
  #city_iのみ攻撃
  if c<=0:
    cnt+=b[i]
  
  #city_i+1も攻撃
  else:
    d=b[i]-a[i+1]-a[i]
    
    #city_i+1生き残る
    if d<=0:
      cnt+=b[i]
      a[i+1]-=c
      
    #オーバキル
    else:
      cnt+=a[i]+a[i+1]
      a[i+1]=0
      
print(cnt)

N = int(input())
lsA = list(map(int,input().split()))
lsB = list(map(int,input().split()))
stA = sum(lsA)
for i in range(N):
    if lsA[i] >= lsB[i]:
        lsA[i] -= lsB[i]
        lsB[i] = 0
    else:
        lsB[i] -= lsA[i]
        lsA[i] = 0
        if lsA[i+1] >= lsB[i]:
            lsA[i+1] -= lsB[i]
            lsB[i] = 0
        else:
            lsA[i+1] = 0
            lsB[i] -= lsA[i+1]
enA = sum(lsA)
print(stA-enA)

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

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

    if B[N-1-i] > 0:
        if A[N-1-i] > B[N-1-i]:
            A[N-1-i] -= B[N-1-i]
            B[N-1-i] = 0
        else:
            B[N-1-i] -= A[N-1-i]
            A[N-1-i] = 0
    # print(A)
    # print(B)
    # print("=============")

print((ans-sum(B)))


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

ans = 0
for i in range(n):
    t1 = min(aa[i], bb[i])
    aa[i] -= t1
    bb[i] -= t1
    ans += t1
    t2 = min(aa[i+1], bb[i])
    aa[i+1] -= t2
    bb[i] -= t2
    ans += t2
print(ans)

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]<=b[i]:
    b[i] = b[i]-a[i]
    a[i]=0
    if a[i+1]<=b[i]:
      a[i+1]=0
    elif a[i+1]>b[i]:
      a[i+1] = a[i+1]-b[i]
  elif a[i]>b[i]:
    a[i] = a[i]-b[i]
#print(num,sum(a))
num = num-sum(a)
print(num)

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

#街の数はn+1個　aiはi番目の街を襲うモンスター
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

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



print(count)


N = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
m = 0
ans = 0
for i in range(N):
    cnt = min(a[i],b[i]+m)
    ans += cnt
    m = min(b[i]+m-cnt,b[i])
ans += min(a[N],m)
print(ans)

import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()
    B = LI()

    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)

def __starting_point():
    resolve()

__starting_point()

N = int(input())
A_ls = list(map(int, input().split(' ')))
B_ls = list(map(int, input().split(' ')))
rst = 0
for i, val in enumerate(B_ls):
    if A_ls[i] + A_ls[i + 1] >= val:
        rst += val
        if A_ls[i] >= val:
            A_ls[i] -= val
        else:
            A_ls[i + 1] -= val - A_ls[i] 
    else:
        rst += A_ls[i] + A_ls[i + 1]
        A_ls[i], A_ls[i + 1] = 0, 0
print(rst)

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
ans = 0
for i in range(N):
    p = A[i] + A[i+1]
    if(p - B[i] >= 0):
        ans += B[i]
        if(A[i] - B[i] < 0):
            A[i+1] = p - B[i]
    else:
        ans += p
        A[i+1] = 0
print(ans)

#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline
import numpy as np

def main():
    n = int(input())
    mob=np.array(list(map(int,input().split())),dtype=int)
    bra=np.array(list(map(int,input().split())),dtype=int)
    ans=0

    for i in range(n):
        ans += min(mob[i],bra[i])
        ans += min(mob[i+1],bra[i]-min(mob[i],bra[i]))
        mob[i+1]-=min(mob[i+1],bra[i]-min(mob[i],bra[i]))
    print(ans)


def __starting_point():
    main()
__starting_point()

#import math
#import itertools
#import numpy as np
#from collections import deque
# sys.setrecursionlimit(10 ** 6)
#mod = 10 ** 9 + 7
#INF = 10 ** 9
#PI = 3.14159265358979323846

INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n=INT()
    A=LIST()
    B=LIST()
    ans=0
    ta=A[0]
    for i in range(n):
        tb=B[i]
        ans+=min(ta,tb)
        ta,tb=max(0,ta-tb),max(0,tb-ta)
        ta+=A[i+1]
        ans+=min(ta,tb)
        ta=max(0,ta-tb)
        ta=min(ta,A[i+1])
        #print(ans,ta,tb)
    print(ans)

def __starting_point():
    do()
__starting_point()

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

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


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

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

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

    # print(total)

print(total)


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

for n in range(N):
  if A[n]>=B[n]:
    A[n]-=B[n]
  else:
    A[n+1] = max(0,A[n+1]+A[n]-B[n])
    A[n] = 0
    
print(M-sum(A))

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

print(ans)

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

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])
  ans += left
  A[i] -= left
  B[i] -= left

  right = min(A[i+1], B[i])
  ans += right
  A[i+1] -= right

print(ans)

n = int(input())
mons = list(map(int, input().split()))
kill = list(map(int, input().split()))

cnt = 0
for i in range(n-1, -1,-1):
    if mons[i+1] < kill[i]:
        cnt += mons[i+1]
        kill[i] -= mons[i+1]
        mons[i+1] = 0
    else:
        cnt += kill[i]        
        mons[i+1] -= kill[i]
        kill[i] = 0
    if mons[i] < kill[i]:
        cnt += mons[i]
        kill[i] -= mons[i]
        mons[i] = 0
    else:
        cnt += kill[i]
        mons[i] -= kill[i]
        kill[i] = 0
        
print(cnt)

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
# 勇者1...1番目・2番目の街で、B_1体まで
# 勇者2...2番目・3番目の街で、B_2体まで
# 勇者N...N番目・(N+1)番目の街で、B_N体まで

# 1番目の街は1番目の勇者が救う
# 1番目の勇者は1番目の街のモンスターを全力でmin(A_1,B_1) 体倒す
# 残った力で2番目の街のモンスターを全力でmin(A_2,B_1−min(A_1,B_1))体倒す
# 2番目の街に残ったモンスターは2番目の勇者が倒す
# これを繰り返す

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

# 初めのモンスター数 - 残ったモンスター数 = 勇者たちが倒した数
ans -= sum(A)
print(ans)

n = int(input())
monster_list = list(map(int, input().split()))
kill_mon = list(map(int, input().split()))

# 配列数を合わせる
kill_mon.append(0)

# 持ち越しパワーの初期化
killed_monster = 0
carry_power = 0
for mons, kill in zip(monster_list, kill_mon):   
    # 全パワーを使い果たす
    if mons >= (kill + carry_power):
        killed_monster += (kill + carry_power)
        # 持ち越しパワーの初期化
        carry_power = 0
    # 余力を残して勝利する
    else:
        killed_monster += mons
        # 前回余力は今回のモンスターにしか使えない。次回のバトルに持ち越せない
        # だから、mons - carry_powerとなる
        # ただ、前回余力が今回モンスターよりも多い場合も考慮するためmax関数を使いマイナス値になることを避ける
        # 前回余力だけで今回バトルに勝利したら、今回パワーをそのまま持ち越せる
        carry_power = kill - max(0, mons - carry_power)
print(killed_monster)

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


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

cnt = 0
for i in range(0,N):
    #隣の町で余った勇者を現在の町へ増援
    #更に隣の町へは増援できないことに注意
    if B[i] <= A[i]:
        cnt += B[i]
    elif B[i] > A[i] and B[i] <= A[i]+A[i+1]:
        cnt += B[i]
        A[i+1] = A[i+1] -B[i] + A[i]
    else:
        cnt += A[i]+A[i+1]
        A[i+1] = 0
        
print(cnt)


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

#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from math import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
import heapq
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
INF = float('inf')
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
    return int(readline())
n = I()
A = readInts()
B = readInts()
ans = 0
for i in range(n):
    v = B[i]
    # kill = A[i] + A[i+1]
    nya = min(v, A[i])
    A[i] -= nya
    v -= nya
    ans += nya
    nya = min(A[i+1], v)
    ans += nya
    A[i+1] -= nya
print(ans)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C
# CreatedDate:  2020-09-04 13:08:51 +0900
# LastModified: 2020-09-04 13:26:51 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


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


def __starting_point():
    main()

__starting_point()

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

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

    ans = 0
    for i in range(len(B)-1, -1, -1):
        m = min(A[i+1], B[i])
        ans += m
        A[i+1] -= m
        B[i] -= m

        m = min(A[i], B[i])
        ans += m
        A[i] -= m
        B[i] -= m

    print(ans)

def __starting_point():
    main()

__starting_point()

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

print(ans)


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

import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    N = I()
    L = LI()
    L2 = LI()
    ans = 0
    for index, num in enumerate(L2):
        if L[index] >= num:
            ans+=num
            L[index]-=num
        else:
            if L[index]+L[index+1] >= num:
                ans+=num
                L[index+1] -= num-L[index]
                L[index] = 0
            else:
                ans += L[index]+L[index+1]
                L[index] = 0
                L[index+1] = 0
    print(ans)
        
    
def __starting_point():
    main()
__starting_point()

n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
first = min(a[0],b[0])
more = min(a[1],b[0]-first)
count = first + more
for i in range(1,n):
    first = min(a[i]-more,b[i])
    more = min(a[i+1],b[i]-first)
    count +=  first + more
print(count)

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

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

n=int(input())
aa=list(map(int,input().split()))
bb=list(map(int,input().split()))
bef=sum(aa)
for i in range(n):
  aa[i] -= bb[i]
  if aa[i] >= 0:
    bb[i]=0
  else:
    bb[i]=0-aa[i]
    aa[i]=0
  aa[i+1] -= bb[i]
  if aa[i+1] >= 0:
    bb[i]=0
  else:
    bb[i]=0-aa[i+1]
    aa[i+1]=0
print((bef-sum(aa)))


def resolve():
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    f = [0] * (n+1)
    f[0] += min(a[0],b[0])
    f[1] += min(a[1],b[0]-f[0])
    a[1] -= f[1]
    for i in range(1,n):
        f[i] += min(a[i],b[i])
        f[i+1] += min(a[i+1],b[i]-min(a[i],b[i]))
        a[i+1] -= f[i+1]
    print(sum(f))
resolve()

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

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.reverse()
B.reverse()
count = 0
for i in range(N):
  if A[i] <= B[i]  <= A[i] + A[i+1]:
    count += B[i]
    A[i], A[i+1] = 0, A[i+1] - (B[i] - A[i])
  elif B[i] > A[i] + A[i+1]:
    count += A[i] + A[i+1]
    A[i], A[i+1] = 0, 0
  elif B[i] < A[i]:
    count += B[i]
    A[i] = A[i] - B[i]
print(count)

from collections import deque
from bisect import bisect_left
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]
    d = B[i] - A[i]
    if d <= A[i+1]:
      cnt += d
      A[i+1] -= d
    else:#つまり d > A[i+1]
      cnt += A[i+1]
      A[i+1] = 0
#  print('i=', i, cnt)

print(cnt)

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

ans = 0
for i in range(n):
    t1 = min(aa[i], bb[i])
    aa[i] -= t1
    bb[i] -= t1
    ans += t1
    t2 = min(aa[i+1], bb[i])
    aa[i+1] -= t2
    ans += t2
print(ans)


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

def __starting_point():
    main()

__starting_point()

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

print(ans)

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

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

for n in range(N):
  if A[n]>=B[n]:
    A[n]-=B[n]
  else:
    A[n+1] = max(0,A[n+1]+A[n]-B[n])
    A[n] = 0
    
print((M-sum(A)))


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

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

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

ans = 0
for i in range(N):
    tmp_l = min(A[i], B[i])
    ans += tmp_l
    A[i] -= tmp_l
    B[i] -= tmp_l
    tmp_r = min(A[i + 1], B[i])
    ans += tmp_r
    A[i + 1] -= tmp_r
    B[i] -= tmp_r
print(ans)


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

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
# 勇者1...1番目・2番目の街で、B_1体まで
# 勇者2...2番目・3番目の街で、B_2体まで
# 勇者N...N番目・(N+1)番目の街で、B_N体まで
 
ans = sum(A)
for i in range(N):
  if A[i] >= B[i]: A[i] -= B[i]
  else:
    # A[i] < B[i]
    B[i] -= A[i]
    A[i] = 0
    A[i+1] = max(A[i+1]-B[i], 0) 

ans -= sum(A)
print(ans)

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

print(ans)

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

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

n = int(input())
la = list(map(int,input().split()))
lb = list(map(int,input().split()))
count = 0
for i in range(n):
  count += min(la[i],lb[i])
  if la[i]<lb[i]:
    if la[i+1] >= lb[i]-la[i]:
      count+=(lb[i]-la[i])
      la[i+1]-=(lb[i]-la[i])
    else:
      count+=la[i+1]
      la[i+1]=0
print(count)

n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split())) + [0]
ans, tmp_b = 0, 0

for i in range(n+1):
    ans += min(al[i], tmp_b+bl[i])
    tmp_b = max(0, bl[i] - max(0, al[i]-tmp_b))
    # print(ans, tmp_b)

print(ans)



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

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

print(total)

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(n):
    if a[i] <= b[i]:
        ans += a[i]
        b[i] -= a[i]
    else:
        ans += b[i]
        b[i] = 0

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

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

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

S_=sum(A)

print(S-S_)

n = int(input())
a_l = list(map(int, input().split()))
b_l = list(map(int, input().split()))

ans = 0
c = 0
for i in range(1, n+1):
    a = a_l[-i]
    if b_l[-i] + c >= a:
        ans += a
        d = c-a
        if d >=0:
            d=0
        c = b_l[-i] - abs(d)
    else:
        ans += b_l[-i] + c
        c = 0

if a_l[0] >= c:
    ans+= c
else:
    ans+= a_l[0]
print(ans) 

#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline
import numpy as np

def main():
    n = int(input())
    mob=np.array(list(map(int,input().split())),dtype=int)
    bra=np.array(list(map(int,input().split())),dtype=int)
    ans=0

    for i in range(n):
        tmp1=min(mob[i],bra[i])
        ans += tmp1
        tmp2=min(mob[i+1],bra[i]-tmp1)
        ans += tmp2
        mob[i+1]-=tmp2
    print(ans)


def __starting_point():
    main()
__starting_point()

n=int(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])
  n1=min(a[i],b[i])
  b[i]-=n1
  a[i+1]=max(0,a[i+1]-b[i]) 
  
print(ans)


# ABC 135 C
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

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

print(ans)

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

print(ans)