Problem:
 Score : 300 points    Problem Statement There are N+1 towns. The i -th town is being attacked by A_i monsters.  We have N heroes. The i -th hero can defeat monsters attacking the i -th or (i+1) -th town, for a total of at most B_i monsters.  What is the maximum total number of monsters the heroes can cooperate to defeat?      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq A_i \leq 10^9  1 \leq B_i \leq 10^9         Input Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_{N+1}  B_1  B_2  ...  B_N       Output Print the maximum total number of monsters the heroes can defeat.        Sample Input 1 2
3 5 2
4 5      Sample Output 1 9  If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.   The first hero defeats two monsters attacking the first town and two monsters attacking the second town.  The second hero defeats three monsters attacking the second town and two monsters attacking the third town.        Sample Input 2 3
5 6 3 8
5 100 8      Sample Output 2 22       Sample Input 3 2
100 1 1
1 100      Sample Output 3 3

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


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

print(kill)

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

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

print(ans)

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

surviver = As[0]
break_number = []

for i in range(N):
    monster_num = As[i+1] + surviver
    hero_num = Bs[i]
    if hero_num > monster_num:
        break_number.append(monster_num)
        surviver = 0
    else:
        break_number.append(hero_num)
        surviver = min(monster_num - hero_num, As[i+1])

print(sum(break_number))

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]
  elif A[i]+A[i+1] >= B[i]:
    ans += B[i]
    A[i+1] -= B[i] - A[i]
  else:
    ans += A[i]+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
c=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]=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 = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

ans = 0

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

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

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

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

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

count = 0

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

print(count)

Solution (Python):



def solv(n, a, b):
    ans = 0
    for i in reversed(range(n)):
        kill = min(a[i+1], b[i])
        ans += kill
        a[i+1] -= kill
        b[i] -= kill

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

    return ans


if __name__ == '__main__':

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

    ans = solv(n, a, b)

    print(ans)

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

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

after = sum(a)

print(before - after)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
cnt = 0
for n in range(N):
    if A[n] <= B[n]:
        cnt += A[n]
        B[n]  -= A[n]
    else:
        cnt += B[n]
        A[n] -= B[n]
        B[n] = 0
    if A[n+1] <= B[n]:
        cnt += A[n+1]
        A[n+1] = 0
    else:
        cnt += B[n]
        A[n+1] -= B[n]
print(cnt)

Solution (Python):
n = int(input())
town = list(map(int,input().split()))
hero = list(map(int,input().split()))
before = sum(town)
for i in range(n):
  townhero = town[n-i] - hero[n-i-1]
  town[n-i] = max( 0, townhero )
  hero[n-i-1] = max( 0, -townhero )
  town[n-i-1] = max( 0, town[n-i-1] - hero[n-i-1] )
after = sum(town)
print( before - after)

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

mons = 0
for i in range(N):

    tmp = min(B[i], A[i])
    mons += tmp
    B[i] -= tmp

    tmp = min(B[i], A[i+1])
    mons += tmp
    A[i+1] -= tmp

print(mons)

Solution (Python):

N = int(input())
A_lst = list(map(int, input().split()))
B_lst = list(map(int, input().split()))

ans = 0

for i in range(N-1, -1, -1):
    if A_lst[i+1] >= B_lst[i]:
        ans += B_lst[i]
        A_lst[i+1] -= B_lst[i]
        B_lst[i] = 0
    else:
        ans += A_lst[i+1]
        B_lst[i] -= A_lst[i+1]
        A_lst[i+1] = 0




    
    if A_lst[i] >= B_lst[i]:
        ans += B_lst[i]
        A_lst[i] -= B_lst[i]
        B_lst[i] = 0
    else:
        ans += A_lst[i]
        B_lst[i] -= A_lst[i]
        A_lst[i] = 0









print(ans)

Solution (Python):

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

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

for ai, bi in zip(a, b):
    if (prev + bi) > ai:
        res += ai
        tmp = ai - prev
        if tmp < 0:
            prev = bi
        else:
            prev = max(0, bi - tmp)
    else:
        res += prev + bi
        prev = 0

print(res)

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

n=int(input())
mon = list(map(int,input().split()))
kni = list(map(int,input().split()))
kill=0

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

print(kill)

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

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

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

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

print(ans)

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

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


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

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

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

Solution (Python):
import sys

fin = sys.stdin.readline
N = [int(elem) for elem in fin().split()][0]
a_list = [int(elem) for elem in fin().split()]
b_list = [int(elem) for elem in fin().split()]


all_sum = 0
prev_b = 0

for i in range(len(a_list[:-1])):
    
    current_a = a_list[i]
    prev_num = min(current_a, prev_b)
    current_a -= prev_num
    current_b = b_list[i]

    
    current_num = min(current_a, current_b)
    
    prev_b = current_b - current_num
    
    all_sum += current_num + prev_num

all_sum += min(a_list[-1], prev_b)
print(all_sum)

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

cnt = 0
flag = False
for i in range(n) :
  if a[i] > b[i] :
    a[i] -= b[i]
    cnt += b[i]
    b[i] = 0
  elif a[i] == b[i]:
    cnt += b[i]
    b[i] = 0
    a[i] = 0
  else :
    b[i] -= a[i]
    cnt += a[i]
    a[i] = 0
    flag = True

  if flag :
    if a[i+1] > b[i] :
      a[i+1] -= b[i]
      cnt += b[i]
      b[i] = 0
    elif a[i+1] == b[i]:
      cnt += b[i]
      b[i] = 0
      a[i+1] = 0
    else :
      b[i] -= a[i+1]
      cnt += a[i+1]
      a[i+1] = 0
    flag = False

print(cnt)

Solution (Python):

"""
Created on Sat Jul 27 20:48:33 2019

@author: ctiuser
"""

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

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

kills = 0

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

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

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

main()

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

Solution (Python):
import math
import fractions
import copy
import numpy as np
def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
"""

def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
"""
n = ip()
a  = iprow()
ans = sum(a)
b = iprow()
pl = 0
pr = n-1
if n==1:
    print(min(sum(b),sum(a)))
    exit(0)

while pl <= pr:
    if b[pl] - a[pl]>0:
        b[pl] -= a[pl]
        a[pl] = 0
    else:
        a[pl]-=b[pl]
        b[pl] = 0
    if b[pl]:
        if b[pl] - a[pl+1]>0:
            b[pl] = 0
            a[pl+1] = 0
        else:
            a[pl+1]-=b[pl]
            b[pl] = 0
    if b[pr] - a[pr+1] > 0:
        b[pr] -= a[pr + 1]
        a[pr+1] = 0
    else:
        a[pr+1] -= b[pr]
        b[pr] = 0
    if b[pr]:
        if b[pr] - a[pr] > 0:
            a[pr] = 0
            b[pr] = 0
        else:
            a[pr] -= b[pr]
            b[pr] = 0
    pl+=1
    pr-=1
print(ans-sum(a))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans_l = 0
rest = A[0]
for i in range(N):
    tmp = B[i]
    if rest < B[i]:
        ans_l += rest
        tmp -= rest
        if A[i+1] < tmp:
            ans_l += A[i+1]
            rest = 0
        else:
            ans_l += tmp
            rest = A[i+1] - tmp
    else:
        ans_l += B[i]
        rest = A[i+1]
ans_r = 0
A_r = A[::-1]
B_r = B[::-1]
rest = A_r[0]
for i in range(N):
    tmp = B_r[i]
    if rest < B_r[i]:
        ans_r += rest
        tmp -= rest
        if A_r[i+1] < tmp:
            ans_r += A_r[i+1]
            rest = 0
        else:
            ans_r += tmp
            rest = A_r[i+1] - tmp
    else:
        ans_r += B_r[i]
        rest = A_r[i+1]
print(max(ans_r, ans_l))

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]
    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):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

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

print(num)

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

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

Solution (Python):
n = int(input())
a_l = [int(_) for _ in input().split(' ')]
a_r = [_ for _ in a_l]

b = [int(_) for _ in input().split(' ')]

ans_l = 0
for i in range(n):
    if b[i] >= a_l[i] + a_l[i + 1]:
        ans_l += a_l[i] + a_l[i + 1]
        a_l[i] = 0
        a_l[i + 1] = 0
    elif b[i] >= a_l[i]:
        ans_l += b[i]
        a_l[i + 1] -= b[i] - a_l[i]
        a_l[i] = 0
    else:
        ans_l += b[i]
        a_l[i] -= b[i]

ans_r = 0
for i in range(n):
    if b[i] >= a_l[i] + a_l[i + 1]:
        ans_r += a_l[i] + a_l[i + 1]
        a_l[i] = 0
        a_l[i + 1] = 0
    elif b[i] >= a_l[i + 1]:
        ans_r += b[i]
        a_l[i] -= b[i] - a_l[i + 1]
        a_l[i + 1] = 0
    else:
        ans_r += b[i]
        a_l[i + 1] -= b[i]

print(max(ans_l, ans_r))

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

def sig(n):
        if n>=0:
                return n
        else:
                return 0

sum_A = sum(A)
for i in range(N):
        hoge = B[i]
        A[i+1] = sig(A[i+1]-sig((B[i]-A[i])))
        A[i] = sig((A[i]-B[i]))

sum_remain = sum(A)
print(sum_A - sum_remain)

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

c = 0
leave = 0

for i, j in zip(a, b):
    atk = j + leave
    c += min(i, atk)
    if leave > i:
        leave = j
    elif leave + j > i:
        leave = j - (i - leave)
    else:
        leave = 0
print(c)

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

MONSTERS = A[:]
BRAVERS = B[:]

sum_monster = sum(MONSTERS)


for i, b in reversed(list(enumerate(BRAVERS))):
    if MONSTERS[i+1] >= b:
        MONSTERS[i+1] -= b
    else:
        b -= MONSTERS[i+1]
        MONSTERS[i+1] = 0
        if MONSTERS[i] >= b:
            MONSTERS[i] -= b
        else:
            MONSTERS[i] = 0
            
ans = sum_monster - sum(MONSTERS)
print(ans)

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

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

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

Solution (Python):
n = int(input())
a_list = list(map(int, input().split(" ")))
b_list = list(map(int, input().split(" ")))

out = 0
for i in range(n):
    if a_list[i] < b_list[i]:
        val0 = a_list[i]
        out += val0
        b_list[i] -= val0
        
        if a_list[i+1] < b_list[i]:
            val1 = a_list[i+1]
            out += val1
            a_list[i+1] = 0
            
        else:
            val1 = b_list[i]
            out += val1
            a_list[i+1] -= val1
    else:
        val0 = b_list[i]
        out += val0
        
        

print(out)

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

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

ans = 0

for i in range(len(b)-1, -1, -1):

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

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

print(ans)

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

ans = 0
"""
ai = 0
bi = 0
while 1:
    if max(b) == 0:
        break
    if a[ai] < b[bi]:
        ans += a[ai]
        b[bi] -= a[ai]
        a[ai] = 0
        ai += 1
    elif a[ai] == b[bi]:
        ans += a[ai]
        b[bi] = 0
        a[ai] = 0
        ai += 1
        bi += 1
    else:
        ans += b[bi]
        a[ai] -= b[bi]
        b[bi] = 0
        if ai > bi:
            bi += 1
        else:
            ai += 1
            bi += 1
 
 
 
    if ai >= n+1 or bi >= n:
        break
print(ans)
"""
for i in range(n):
    if b[i] >= a[i] + a[i+1]:
        ans += a[i] + a[i+1]
        a[i+1] = 0
    else:
        if b[i] >= a[i]:
            ans += b[i]
            a[i+1] -= (b[i]-a[i])
        else:
            ans += b[i]
print(ans)

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

Solution (Python):
N = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))

k = 0
for i in range(N):
    j = N - i - 1
    bi = bl[j]
    ai1 = al[j+1]
    if bi <= ai1:
        k += bi
        continue
    elif bi > ai1:
        k += ai1
        bi -= ai1
        ai = al[j]
        if bi <= ai:
            k += bi
            al[j] -= bi
            continue
        elif bi > ai:
            k += ai
            al[j] = 0
            continue

print(k)

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

Solution (Python):
import sys

def input():
    return sys.stdin.readline().rstrip()

if __name__ == "__main__":
    n = int(input())
    al = list(map(int, input().split()))
    bl = list(map(int, input().split()))

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

    print(total)

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

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

guchoku=0
for i,b in enumerate(B):
    t = A[i] - b
    if t <= -1:
        zen = max(A[i+1] + t, 0)
        guchoku += A[i+1] - zen
        A[i+1] = zen
    guchoku += min(A[i], b)
print(guchoku)

Solution (Python):


import math
import sys
import itertools
import numpy as np


n =int(input())


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


print(max_mon)

Solution (Python):
def sol():
    import sys
    input=sys.stdin.readline
    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]
            ans+=min(b[i],a[i+1])
            a[i+1]=max(0,a[i+1]-b[i])
        else:
            ans+=b[i]
    print(ans)

if __name__=="__main__":
    sol()

Solution (Python):

def main():
  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] >= 0:
      cnt += A[i]
      B[i] -= A[i]
      A[i] = 0
    elif B[i] - A[i] < 0:
      cnt += B[i]
      tmp = B[i]
      B[i] = 0
      A[i] -= tmp
    if B[i] == 0:
      continue
    if B[i] - A[i+1] >= 0:
      cnt += A[i+1]
      B[i] -= A[i+1]
      A[i+1] = 0
    elif B[i] < A[i+1]:
      cnt += B[i]
      tmp = B[i]
      B[i] = 0
      A[i+1] -= tmp
  print(cnt)

if __name__ == "__main__":
  main()

Solution (Python):
N = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

total = 0
for i in range(N):
    b = b_list[i]

    defeated = min(a_list[i], b)
    a_list[i] -= defeated
    b -= defeated
    total += defeated

    defeated = min(a_list[i + 1], b)
    a_list[i + 1] -= defeated
    total += defeated
print(total)

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0
hikitugi = 0

for ind,i in enumerate(B):
    if A[ind] <= i:
        ans += A[ind]
        hikitugi = i - A[ind]
        if A[ind+1] <= hikitugi:
            ans += A[ind+1]
            A[ind+1] = 0
        else:
            ans += hikitugi
            A[ind+1] = A[ind+1] - hikitugi
    else:
        A[ind] = A[ind] - i
        ans += i
print(ans)

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

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

print(cnt)

Solution (Python):

import sys

import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n = readi()
a = readl()
b = readl()
count = 0
for i in range(n):
    for j in range(2):
        t = min(a[i + j], b[i])
        count += t
        a[i + j] -= t
        b[i] -= t
print(count)

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

print(count)

Solution (Python):
import sys

sys.setrecursionlimit(10**9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def LIST(): return [int(x) for x in input().split()]
MOD = 10**9 + 7

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

kill = 0

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

print(kill)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
a = list(li())
b = list(li())

cnt = 0

for i in range(n+1):
    for j in range(i-1, i+1):
        if j < 0 or j >= n:
            continue

        if b[j] == 0:
            continue

        elif a[i] <= b[j]:
            cnt += a[i]
            b[j] -= a[i]
            a[i] = 0
            break

        else:
            cnt += b[j]
            a[i] -= b[j]
            b[j] = 0


print(cnt)

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