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
sys.setrecursionlimit(300000)


def solve(N: int, A: "List[int]", B: "List[int]"):
    ret = 0
    for i in range(N):
        if A[i] <= B[i]:
            B[i] -= A[i]
            ret += A[i]
            tmp = min(A[i + 1], B[i])
            
            A[i + 1] -= tmp
            ret += tmp
        else:
            A[i] -= B[i]
            ret += B[i]
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [ int(next(tokens)) for _ in range(N+1) ]  
    B = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, A, B)

if __name__ == '__main__':
    main()

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

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

sumA = sum(As)

for i, B in enumerate(Bs):
    d = min(As[i], B)
    As[i] -= d
    B -= d
    d = min(As[i+1], B)
    As[i+1] -= d
    B -= d

print(sumA - sum(As))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
for i in range(n):
    for j in range(2):
        if a[i+j] > b[i]:
            cnt += b[i]
            a[i+j] -= b[i]
            b[i] = 0
        else:
            cnt += a[i+j]
            b[i] -= a[i+j]
            a[i+j] = 0
print(cnt)

Solution (Python):



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


tot=0

for i in range(n):
	kl=min(a[i],b[i])

	b[i]-=kl
	tot+=kl
	a[i]-=kl

	kl=min(a[i+1],b[i])
	b[i]-=kl
	tot+=kl
	a[i+1]-=kl

print(tot)

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
a_list=list(map(int,input().split()))
b_list=list(map(int,input().split()))
ans=0
for i in range(n):
    if b_list[i]<=a_list[i]:
        ans+=b_list[i]
    else:
        ans+=a_list[i]
        b=b_list[i]-a_list[i]
        if b<=a_list[i+1]:
            ans+=b
            a_list[i+1]-=b
        else:
            ans+=a_list[i+1]
            a_list[i+1]=0
print(ans)

Solution (Python):

import sys


def solve(N: int, A: "List[int]", B: "List[int]"):

    n = 0
    for i in range(len(B)):
        n1 = min(A[i],B[i])
        n += n1
        B[i] -= n1
        n2 = min(A[i+1],B[i])
        n += n2
        A[i+1] -= n2
    print(n)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [ int(next(tokens)) for _ in range(N+1) ]  
    B = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, A, B)

if __name__ == '__main__':
    main()

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

res = 0
for n in range(N):
    b = min(A[n], B[n])
    B[n] -= b
    b2 = min(A[n + 1], B[n])
    A[n + 1] -= b2
    res += b + b2

print(res)

Solution (Python):

from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


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

    td = 0

    for i, j in enumerate(b):
        mi = min(a[i], j)
        td += mi + min(j - mi, a[i + 1])
        a[i] -= mi
        a[i + 1] -= min(j - mi, a[i + 1])

    print(td)




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

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

ans = 0
for i in range(N):
    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)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
B = LIST()

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)

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

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

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

ans = 0

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

print(ans)

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

ans = 0
for i, bb in enumerate(b):
    use = min(a[i], b[i])
    ans += use
    a[i] -= use
    b[i] -= use

    if b[i] == 0:
        continue

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

print(ans)

Solution (Python):
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]






N = int(input())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))

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

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

Solution (Python):
import sys


fin = sys.stdin.readline
N = int(fin())
A_list = [int(elem) for elem in fin().split()]
B_list = [int(elem) for elem in fin().split()]
max_num_beaten = 0
for i, B in enumerate(B_list):
    cur_power = B
    A0 = A_list[i]
    num_beaten = min(A0, cur_power)
    A_list[i] -= num_beaten
    max_num_beaten += num_beaten
    cur_power -= num_beaten

    A1 = A_list[i + 1]
    num_beaten = min(A1, cur_power)
    A_list[i + 1] -= num_beaten
    max_num_beaten += num_beaten

print(max_num_beaten)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
num = 0
p = 0
for i in range(N):
    a = A[i]
    if a <= p:
        num += a
        p = B[i]
        continue
    a -= p
    num += p
    if a <= B[i]:
        num += a
        p = B[i] - a
        continue
    num += B[i]
    p = 0
else:
    num += min(p, A[-1])
print(num)

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

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

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

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

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

print(ans)

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

ans += min(pre, A[N])
print(ans)

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

Solution (Python):



import itertools as ite
import math
import random
import sys
import codecs
from collections import defaultdict

INF = 10 ** 18
MOD = 10 ** 9 + 7

sys.setrecursionlimit(1000000)

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

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

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

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

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

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

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

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

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

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

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

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

    c += e + d

print(c)

Solution (Python):
N = int(input())
towns = list(map(int, input().split()))
heroes = list(map(int, input().split()))

res = 0
for i in range(N):
  x = min(heroes[i], towns[i])
  towns[i] -= x
  heroes[i] -= x
  res += x
  x = min(heroes[i], towns[i + 1])
  towns[i + 1] -= x
  heroes[i] -= x
  res += x

print(res)

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

Solution (Python):
import sys
input = sys.stdin.readline

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

    x=min(A[i+1],B[i])
    ANS+=x
    A[i+1]-=x
    B[i]-=x

print(ANS)

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

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

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline

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

if __name__ == "__main__":
    main()

Solution (Python):
N=int(input())
M=list(map(int,input().strip().split(' ')))
H=list(map(int,input().strip().split(' ')))
count=0
for i in range(len(H)):
    temp=min(M[i],H[i])
    H[i]-=temp
    M[i]-=temp
    count+=temp
    temp2=min(M[i+1],H[i])
    H[i]-=temp2
    M[i+1]-=temp2
    count+=temp2
print(count)

Solution (Python):
n = int(input())
lis = list(map(int,input().split()))
li = list(map(int,input().split()))
ans = 0
for i in range(n):
    num = min(lis[i],li[i])
    ans += num
    lis[i] -= num
    li[i] -= num
    nu = min(lis[i+1],li[i])
    ans += nu
    lis[i+1] -= nu
    li[i] -= nu
print(ans)

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

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

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

ans = 0

for i in range(n):
    if a[i] > b[i]:
        ans += b[i]

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

print(ans)

Solution (Python):
inpl = lambda: list(map(int,input().split()))
N = int(input())
A = inpl()
B = inpl()
ans = 0
for i in range(N):
    p = min(A[i],B[i])
    B[i] -= p
    q = min(B[i],A[i+1])
    A[i+1] -= q
    ans += p + q
print(ans)

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

print(ans)

Solution (Python):
from collections import Counter, defaultdict

def main():
    num = int(input())
    data_teki = list(map(int, input().split()))
    data_mikata = list(map(int, input().split()))

    ans = min(data_mikata[-1], data_teki[-1])
    nokori = data_mikata[-1] - ans
    for i in range(num)[::-1]:
        if i == 0:
            taoseru = nokori
        else:
            taoseru = nokori + data_mikata[i - 1]
        taosu  = min(data_teki[i], taoseru)
        ans += taosu
        nokori = min(data_mikata[i - 1], taoseru - taosu)
        

    print(ans)







if __name__ == '__main__':
    main()

Solution (Python):
LI=lambda:list(map(int,input().split()))
MI=lambda:map(int,input().split())
I=lambda:input()
ans=0
n=int(I())
a,b=LI(),LI()
for i in range(n):
  c=min(a[i],b[i])
  ans+=c
  b[i]-=c
  c=min(a[i+1],b[i])
  ans+=c
  a[i+1]-=c
print(ans)

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

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

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():
    N = II()
    A = LMIIS()
    B = LMIIS()
    count = 0
    for i in range(N-1,-1,-1):
        if A[i+1] <= B[i]:
            count += A[i+1]
            B[i] -= A[i+1]
            if A[i] <= B[i]:
                count += A[i]
                A[i] = 0
            else:
                count += B[i]
                A[i] -= B[i]
        else:
            count += B[i]
    print(count)





    pass
if __name__ == '__main__':
    main()

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

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

print(ans)

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

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

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

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

print(ans)

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

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

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

print(res)

Solution (Python):

import sys
INF = float("inf")


def solve(N: int, A: "List[int]", B: "List[int]"):
    count = 0
    for i in range(N):
        b = min(A[i], B[i])
        count += b
        if A[i] < B[i]:
            c = min(B[i]-A[i], A[i+1])
            count += c
            A[i+1] -= c
    print(count)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N+1)]  
    B = [int(next(tokens)) for _ in range(N)]  
    solve(N, A, B)


if __name__ == '__main__':
    main()

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    
    n = int(input())
    arr = list(map(int, input().split()))
    b = list(map(int, input().split()))
    
    ans = 0
    for i in range(n):
        if arr[i] <= b[i]:
            ans += arr[i]
            b[i] -= arr[i]
            if arr[i + 1] <= b[i]:
                ans += arr[i + 1]
                arr[i + 1] = 0
            else:
                ans += b[i]
                arr[i + 1] -= b[i]
        else:
            ans += b[i] 
    
    print(ans)
    
    return 0

main()

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for i, b in enumerate(B):
    an = min(b, A[i])
    b -= an
    A[i] -= an
    ans += an

    an_ = min(b, A[i+1])
    A[i+1] -= an_
    ans += an_
print(ans)

Solution (Python):
import queue

"""
N = int(input())



A = []
for i in range(N):
	A.append(int(input()))

print(A)

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

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

Solution (Python):
n = int(input())
aaa = list(map(int, input().split()))
sa = sum(aaa)
bbb = list(map(int, input().split()))
for i, b in enumerate(bbb):
    c = min(aaa[i], b)
    aaa[i] -= c
    aaa[i + 1] -= min(aaa[i + 1], b - c)
print(sa - sum(aaa))