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):
import sys,collections,math,random;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

n = I()
alis = Is()
blis = Is()
ans = 0
for i in range(n):
    if alis[i] >= blis[i]:
        ans += blis[i]
    elif alis[i] + alis[i+1] <= blis[i]:
        ans += alis[i] + alis[i+1]
        alis[i+1] = 0
    else:
        ans += blis[i]    
        alis[i+1] = max(0, alis[i] + alis[i+1] - blis[i])
print(ans)

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

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

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

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

a=input().split(" ")
b=input().split(" ")
A=[]
B=[]
for i in a:
    A.append(int(i))
for i in b:
    B.append(int(i))
count=0
roof=0
for i in range(len(A)-1):
    enemy=A[i]
    if(roof>0):
        if(enemy>roof):
            count+=roof
            enemy-=roof
        else:
            count+=enemy
            enemy=0
            roof=0
    if(enemy<B[i]):
        count+=enemy
        roof=B[i]-enemy
    else:
        count+=B[i]
        roof=0
enemy=A[-1]
if(roof>0):
    if(enemy>roof):
        count+=roof
        enemy-=roof
    else:
        count+=enemy

print(count)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
prev = 0
count = 0
for i, a in enumerate(A):
  if i >= N:
    if A[i] < prev:
      count += A[i]
      continue
    else:
      count += prev
      continue

  if prev >= a:
    count += a
    prev = B[i]
    continue
  
  if prev + B[i] >= a:
    count += a
    temp_a = a - prev
    prev = B[i] - temp_a
    continue
  else:
    count += B[i]+prev
    prev =0
  
print(count)

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

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

Solution (Python):

N=int(input())
M=list(map(int,input().split()))
S=list(map(int,input().split()))
K=[0 for i in range(N)]
for i in range(N):
  if M[i]>=S[i]:
    K[i]=S[i]
  else:
    if M[i+1]>=S[i]-M[i]:
      M[i+1]-=S[i]-M[i]
      K[i]=S[i]
    else:
      K[i]=M[i]+M[i+1]
      M[i+1]=0
print(sum(K))

Solution (Python):
import numpy as np

N=int(input())
A=np.array(input().split(),dtype=np.int32)
B=np.array(input().split(),dtype=np.int32)

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

print(result)

Solution (Python):
n=int(input())
Ai=list(map(int,input().split()))
Bi=list(map(int,input().split()))

ans=0
for i in range(n):
    b=Bi[i]
    if i<n-1:
        if Ai[i]>=b:
            ans+=b
        else:
            ans+=Ai[i]
            b-=Ai[i]
            if Ai[i+1]>=b:
                ans+=b
                Ai[i+1]-=b
            else:
                ans+=Ai[i+1]
                Ai[i+1]=0
    else:
        ans+=min(Ai[i]+Ai[i+1], b)


print(ans)

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

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

Solution (Python):

import sys;
import string;
sys.setrecursionlimit(20000) 

import copy
import math
import itertools

from functools import lru_cache


PPP = 1000000007


n = int(input())








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




























import re
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

def ton(n, k, x):
  s = ""
  while n > 0:
    s = str(n%k) + s
    n //= k
  return s





def getEnergy():
  for y in b:
    yield y

def doit():
  begin = 0
  end = n
  ng = 0
  total = 0
  for i in range(n):
    ab = min(a[i], b[i])
    total += ab
    a[i] -= ab
    b[i] -= ab
    ba = min(a[i+1], b[i])
    total += ba
    a[i+1] -= ba
    b[i] -= ba
  
  for i in range(n):
    i2 = n-1-i
    ab = min(a[i2+1], b[i2])
    total += ab
    a[i2+1] -= ab
    b[i2] -= ab
    ba = min(a[i2], b[i2])
    total += ba
    a[i2] -= ba
    b[i2] -= ba
  
  return total

def yn():
  if doit():
    return "YES"
  else:
    return "NO"




try:
  
  print(doit())
  
except Exception as e:
  print("e", e, file=sys.stderr)

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

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

print(sum)

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

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

print(count)

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

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

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

print(ans)

Solution (Python):

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


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

print(monstar_count)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys, math, bisect, random, itertools
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


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


def B():
    n = II()
    p = LI()
    for i in range(n):
        for k in range(i+1, n):
            pi = p[::1]
            pi[i], pi[k] = pi[k], pi[i]
            for j in range(n-1):
                if pi[j] > pi[j + 1]:
                    break
            else:
                print("YES")
                return
    print("NO")
    return


def C():
    n = II()
    a = LI()
    b = LI()
    ans = 0
    for i in range(n):
        if a[i] >= b[i]:
            ans += b[i]
            continue
        ans += a[i]
        b[i] -= a[i]
        if a[i + 1] >= b[i]:
            ans += b[i]
            a[i + 1] -= b[i]
            continue
        ans += a[i + 1]
        a[i + 1] = 0
    print(ans)
    return


def D():
    s = S()
    s = s[::-1]
    dp = [[0] * 13 for _ in range(len(s))]
    remain = 1
    if s[0] == "?":
        for i in range(10):
            dp[0][i] = 1
    else:
        dp[0][int(s[0])] = 1
    for i in range(1, len(s)):
        remain = (remain * 10) % 13
        dpi = dp[i]
        dpi1 = dp[i-1]
        if s[i] == "?":
            for k in range(10):
                for j in range(13):
                    dpi[(j + remain * k) % 13] += dpi1[j]
                    dpi[(j + remain * k) % 13] %= mod
        else:
            for j in range(13):
                dpi[(j + remain * int(s[i])) % 13] += dpi1[j]
    print(dp[-1][5])
    return


def E():
    return


def F():
    return


if __name__ == '__main__':
    C()

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

count = 0

for num in range(N):
    if A[num] >= B[num]:
        num_dead_monster = B[num]
    else:
        num_dead_monster = A[num]
    
    count += num_dead_monster
    A[num] -= num_dead_monster
    B[num] -= num_dead_monster
    
    if B[num] >= 1:
        if A[num+1] >= B[num]:
            num_dead_monster = B[num]
        else:
            num_dead_monster = A[num+1]
        count += num_dead_monster
        A[num+1] -= num_dead_monster
        
    
    
    
    

print(count)

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.reverse()
B.reverse()
ans = 0
t = A[0]
for a, b in zip(A[1:], B):
    t += a
    m = min(t, b)
    ans += m
    t = t - m if t - a <= b else a

print(ans)

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

totalMonster = 0
kurikoshi = 0
for i in range(N + 1):
    if i == N:
        if kurikoshi >= A[i]:
            totalMonster = totalMonster + A[i]
        else:
            totalMonster = totalMonster + kurikoshi
        break

    if kurikoshi >= A[i]:
        totalMonster = totalMonster + A[i]
        kurikoshi = B[i]
    elif kurikoshi + B[i] >= A[i]:
        totalMonster = totalMonster + A[i]
        kurikoshi = kurikoshi + B[i] - A[i]
    else:
        totalMonster = totalMonster+ kurikoshi + B[i]
        kurikoshi = 0

print(totalMonster)

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

miss = 0
ex = 0
target = 0

for i in range(n+1):
    target = max(a[i]-ex, 0)
    if b[i] > target:
        ex = b[i]-target
    else:
        miss += target-b[i]
        ex = 0

print(sum(a) - miss)

Solution (Python):
n = int(input())
list_a=list(map(int, input().split()))  
list_b=list(map(int, input().split()))  
count = 0
list_b.append(0)
yuu_amari = 0
for i in range(n):
    if list_a[i] > yuu_amari:
        mon_now = list_a[i] - yuu_amari
    else:
        mon_now = 0
    yuu_now = list_b[i]
    count += min(mon_now, yuu_now)
    
    if yuu_now - mon_now > 0:
        yuu_amari = yuu_now - mon_now
        count += min(list_a[i+1], yuu_amari)
    else :
        yuu_amari = 0
print(count)

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

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

tmp = min(A[N - 1], B[N])
count += tmp
B[N] -= tmp
A[N - 1] -= tmp

print(count)

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

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

    else:
        if A[i] - (B[i] + tmp) < 0:
            sum += A[i]
            A[i] = A[i] - tmp
            if A[i] < 0:
                A[i] = 0
            tmp = B[i] - A[i]
        else:
            sum += B[i] + tmp
            tmp = 0

print(sum)

Solution (Python):
n = int(input())
enemies = list(map(int, input().split()))
powers = list(map(int, input().split()))
powers.append(0)
nokori = 0
cnt = 0


for i in range(n+1):
  cnt += min(enemies[i], powers[i]+nokori)
  if powers[i]+nokori > enemies[i]:
    if nokori >= enemies[i]:
      nokori = powers[i]
    else:
      nokori = powers[i]+nokori-enemies[i]
  else:
    nokori = 0

print(cnt)

Solution (Python):
n = int(input())
city_backup = list(map(int, input().split()))
player = list(map(int, input().split()))
ans = 0
start_sum, end_sum = 0, 0
city = city_backup
for i in city:start_sum+=i
for i in range(n):
    
    if player[i]>city[i]:
        city[i+1] -= player[i] - city_backup[i]
        if city[i+1] < 0:
            city[i+1] = 0
        city[i] = 0
    else:city[i] -= player[i]
for i in city:end_sum+=i
print(start_sum-end_sum)

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

C = []
D = []
for n in range(N+1):
    if n == 0:
        C.append(min(A[0], B[0]))
        D.append(max(0, B[0] - A[0]))
    elif n == N:
        C.append(min(A[N], D[N-1]))
    else:
        C.append(min(A[n], B[n] + D[n-1]))
        D.append(max(0, B[n] - max(0, A[n] - D[n-1])))

print(sum(C))

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

Solution (Python):
import copy

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

d_A = copy.deepcopy(A)

ans = 0

for i in range(N):

    if A[i] <= B[i]:
        B[i] -= A[i]
        A[i] = 0
    else:
        A[i] -= B[i]
        B[i] = 0

    if B[i] > 0 and A[i+1] > 0:
        A[i+1] -= min(B[i], A[i+1])


ans = sum([_a - a for a,_a in zip(A, d_A)])


print(ans)

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

cnt = 0
i = 0
while i < N:
    if A[i] <= B[i]:
        cnt += A[i]
        if A[i+1] >= (B[i] - A[i]):
            cnt += (B[i] - A[i])
            A[i+1] = A[i+1] - (B[i] - A[i])
        else:
            cnt += A[i+1]
            A[i+1] = 0
        i += 1

    else:
        cnt += B[i]
        i += 1

print(cnt)

Solution (Python):
N = int(input()) 
B= [int(x) for x in input().split()]
A= [int(x) for x in input().split()]
all_ene = sum(B)
c = 0
for i in range(N):
  if B[i] >= A[i]:
    B[i] = B[i]-A[i]
  else:
    B[i+1] = max(0,B[i+1]+B[i]-A[i])
    B[i] = 0

print(all_ene-sum(B))

Solution (Python):
N = int(input())
alist = [int(i) for i in input().split(" ")]
blist = [int(i) for i in input().split(" ")]
counts = 0
if alist[0] < blist[0]:
  counts += alist[0]
  blist[0] -= alist[0]
else:
  counts += blist[0]
  blist[0] = 0
for i in range(1,N):
  if alist[i] < blist[i-1]:
    counts += alist[i]
  elif alist[i] < blist[i-1] + blist[i]:
    counts += alist[i]
    blist[i] -= alist[i] - blist[i-1]
  else:
    counts += blist[i-1] + blist[i]
    blist[i] = 0
counts += min(alist[-1], blist[-1])
print(counts)

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

Solution (Python):
n = int(input())
enemies = list(map(int, input().split()))
heros = list(map(int, input().split()))

enemy = enemies[0]
count = 0
diff = 0

for i in range(n):
    enemy = enemies[i]

    kill = min(enemy, diff)
    count += kill
    enemy -= kill

    kill = min(enemy, heros[i])
    count += kill
    diff = heros[i] - kill

count += min(enemies[n], diff)
print(count)

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

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

print(dp[-1])

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



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

    elif i!=N:
        
        taosita1 = B[i-1]-max((B[i-1]-A[i]),0)
        count =count + taosita1
        A[i] = A[i]-taosita1
        
        taosita2 = B[i]-max((B[i]-A[i]),0)
        count =count + taosita2
        B[i]= B[i]-taosita2
    if i == N:
        
        taosita3 = B[i-1]-max((B[i-1]-A[i]),0)
        count =count + taosita3

print(count)

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

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

print(COUNT)

Solution (Python):
a=[]
b=[]
ah=[]
bh=[]
c=int(input())
for x in input().split(" "):
    a.append(int(x))
for x in input().split(" "):
    b.append(int(x))

i=0
n=0

while i<c:
    if a[i]>=b[i]:
        n+=b[i]
        a[i]=a[i]-b[i]
    else:
        n+=a[i]
        b[i]-=a[i]
        a[i]=0
        if a[i+1]>=b[i]:
            n+=b[i]
            a[i+1]-=b[i]
        else:
            n+=a[i+1]
            a[i+1]=0
    i+=1

print(n)

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





a = A[::-1]
b = B[::-1]



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

Solution (Python):


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

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

    print(ans)


if __name__ == "__main__":
    solve()

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

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

print(enemy)

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

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

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

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

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

print(an)

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 += a[i]
        d = b[i] - a[i]

        if a[i + 1] < d:
            count += a[i + 1]
            a[i + 1] = 0
        else:
            count += d
            a[i + 1] -= d

    else:
        count += b[i]

print(count)

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

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

mon=0
amari=0
for i in range(n):
    if a[i]<=amari:
        mon=mon+a[i]
        amari=b[i]
        continue
    if a[i]>=b[i]+amari:
        mon=mon+b[i]+amari
        amari=0
    if a[i]<b[i]+amari:
        mon=mon+a[i]
        amari=b[i]+amari-a[i]

if a[n]>amari:
    mon=mon+amari
if a[n]<=amari:
    mon=mon+a[n]

print(mon)

Solution (Python):


import os
import sys

N = int(input())

A = list(map(lambda x: int(x), input().split(" ")))

B = list(map(lambda x: int(x), input().split(" ")))





total = 0

for i in range(N):
	if B[i] < A[i]:
		mons = B[i]
		total += mons

	else:
		mons = A[i]
		total += mons

		B[i] -= mons
		
		if B[i] > 0:
			mons = min(A[i+1], B[i])
			total += mons

			B[i] -= mons
			A[i+1] -= mons

print(total)

Solution (Python):
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))

res=0

for i in range(n):
	if y[i]<x[i]:
		res+=y[i]
	else:
		if x[i+1]-(y[i]-x[i])>0:
			res+=y[i]
			x[i+1]=x[i+1]-(y[i]-x[i])
		else:
			res+=x[i]+x[i+1]
			x[i+1]=0

print(res)

Solution (Python):
"""
AtCoder
    :author: vierbein
"""
from __future__ import absolute_import, division, print_function



import sys


def pp(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)


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

ans = 0
rest = A[0]
for a, b in zip(A[1:], B):
    inc = min(a + rest, b)
    ans += inc
    rest = max(0, a - max(0, b - rest))
print(ans)

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

a = list(int(_) for _ in input().split()) 
b = list(int(_) for _ in input().split()) 

cnt = min(a[0],b[0])
if b[0] > a[0]:
	tmp = b[0] - a[0]
else:
	tmp = 0

for i in range(1,n):

	if tmp >= a[i]:
		tmp =  b[i]
		cnt += a[i]
	elif b[i]+tmp >= a[i]:
		tmp = b[i]+tmp - a[i]
		cnt += a[i]
	else:
		cnt += b[i]
		cnt += tmp
		tmp = 0

if tmp > 0:
	cnt += min(a[n],tmp)

print(cnt)

Solution (Python):
from enum import Enum
from queue import Queue
import collections

import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

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

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

Solution (Python):
N=int(input())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
start =sum(a)
for i in range(N):
    if a[i]<=b[i]:
        b[i]-=a[i]
        a[i]=0
    elif a[i]>b[i]:
        a[i]-=b[i]
        b[i]=0
    if a[i+1]<=b[i]:
        b[i]-=a[i+1]
        a[i+1]=0
    elif a[i+1]>b[i]:
        a[i+1]-=b[i]
        b[i]=0
end= sum(a)
print(start-end)

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

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

Solution (Python):
n = int(input())
a_li = list(map(int,input().split()))
b_li = list(map(int,input().split()))

cnt = 0
for i in range(n):
    if b_li[i] >= a_li[i]:
        cnt += a_li[i]
        b_li[i] -= a_li[i]
    else :
        cnt += b_li[i]
        b_li[i] = 0
        continue
    
    if b_li[i] >= a_li[i + 1]:
        cnt += a_li[i + 1]
        a_li[i + 1] = 0
    else :
        cnt += b_li[i]
        a_li[i + 1] -= b_li[i]
print(cnt)

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

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

print(beat)

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


amari = 0
total = sum(A)

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

amari += A[N]

print(total - amari)

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

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

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

print(cnt)

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


if __name__ == "__main__":
    n = int(input())
    a_list = li()
    b_list = li()

    monster = 0
    for index in range(n):
        monster_add = min(a_list[index], b_list[index])
        monster = monster + monster_add
        a_list[index] = a_list[index] - monster_add
        b_list[index] = b_list[index] - monster_add

        monster_add = min(a_list[index + 1], b_list[index])
        monster = monster + monster_add
        a_list[index + 1] = a_list[index + 1] - monster_add
        b_list[index] = b_list[index] - monster_add
    print(monster)

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

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

print(count)