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(" ")))

cnt = 0

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

print(cnt)

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

a = [] 
a_li = input().split()

b = []
b_li = input().split()

for i in range(n):
    a.append(int(a_li[i]))
    b.append(int(b_li[i]))
a.append(int(a_li[n]))

ans = 0

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

print(ans)

Solution (Python):
n = int(input())
mon = list(map(int, input().split()))
bra = list(map(int, input().split()))

sup = 0
total = 0
for i in range(n):
    spec = bra[i]+sup
    if mon[i]>spec:
        total += spec
        sup = 0
    else:
        total += mon[i]
        if sup>=mon[i]:
            sup = bra[i]
        else:
            sup = spec - mon[i]

if (sup>mon[n]):
    total += mon[n]
else:
    total += sup

print(total)

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

sum_A = sum(A)

for n in reversed(range(N)):
    beated = min(B[n], A[n+1])
    B[n] -= beated
    A[n+1] -= beated

    beated = min(B[n], A[n])
    B[n] -= beated
    A[n] -= beated

print(sum_A - sum(A))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
power = sum(B)
def draw(a, b):
    ba = a-b
    if ba < 0 :
        return 0, -ba
    else:
        return ba, 0

for i in range(N):
    B[i],A[i] = draw(B[i], A[i])
    A[i+1], B[i] = draw(A[i+1],B[i])
print(power - sum(B))

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

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

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

counter = 0
left_num = 0
for i,(one,two) in enumerate(zip(monster_num,hero_power)):
    counter += min(two+left_num,one)
    left_num = min(max(0,two+left_num-one),two)
counter += min(monster_num[-1],left_num)

print(counter)

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

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

Solution (Python):
N = int(input())
A_list = list(map(int,input().split()))
B_list = list(map(int,input().split()))

result = 0
for i in range(0,N):
    if A_list[i] < B_list[i]:
        result += A_list[i]
        if A_list[i+1] < B_list[i] - A_list[i]:
            result += A_list[i+1]
            A_list[i+1] = 0
        else:
            result += B_list[i] - A_list[i]
            A_list[i+1] = A_list[i+1] - (B_list[i] - A_list[i])
    else:
        result += B_list[i]
        

'''
B_odd = B_list[N-1] - A_list[N-1]
if A_list[N] > B_odd:
    result += B_odd
    print(B_odd)
else:
    result += A_list[N]
    print(A_list[N])
'''
print(result)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
c=0
for n in range(N):
    ys=N-1-n
    machi=N-n
    if A[machi]>B[ys]:
        c=c+B[ys]
    else:
        c=c+A[machi]
        B[ys]=B[ys]-A[machi]
        if A[machi-1]>B[ys]:
            c=c+B[ys]
            A[machi-1]=A[machi-1]-B[ys]
        else:
            c=c+A[machi-1]
            B[ys]=B[ys]-A[machi-1]
            A[machi-1]=0
print(c)

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

beats = list()
count = 0
for i in range(n):
    beat = min(Bs[i], As[i])
    count += beat
    Bs[i] -= beat
    As[i] -= beat

    beat = min(Bs[i], As[i + 1])
    count += beat
    Bs[i] -= beat
    As[i + 1] -= beat

    
    
    
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]
        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)

Solution (Python):
n = int(input())
a = list(map(int,input().split(" ")))
b = list(map(int,input().split(" ")))
fb = sum(b)
for i in range(n):
  tmp = min(a[i],b[i])
  a[i] -= tmp
  b[i] -= tmp
  tmp = min(a[i+1],b[i])
  a[i+1] -= tmp
  b[i] -= tmp
result = fb - sum(b)
print(result)

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

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

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

rest = 0
c = 0
for i in range(len(Bs)):
    if As[i] >= rest:
        ene = As[i] - rest
        c += rest
        if ene >= Bs[i]:
            c += Bs[i]
            rest = 0
        else:
            c += ene
            rest = Bs[i] - ene
    else:
        c += As[i]
        rest = Bs[i]
if As[i+1] >= rest:
    c += rest
else:
    c += As[i+1]
print(c)

Solution (Python):
n = int(input())
la = list(map(int, input().split()))
lb = list(map(int, input().split()))

c = 0
for i in range(n):
    m = la[i] + la[i+1]
    if la[i] >= lb[i]:
        c += lb[i]
    elif m > lb[i]:
        la[i+1] = m - lb[i]
        c += lb[i]
    else:
        la[i+1] = 0
        c += m

print(c)

Solution (Python):
N=int(input())
An=list(map(int,input().split()[:N+1]))
Bn=list(map(int,input().split()[:N]))

ret=0

for i in range(N,0,-1):
    tmp=Bn[i-1]
    if An[i] >= tmp:
        An[i]-=tmp
        ret+=tmp
        continue
    else:
        tmp-=An[i]
        ret+=An[i]
        An[i]=0
        if An[i-1]>=tmp:
            An[i-1]-=tmp
            ret+=tmp
        else:
            ret+=An[i-1]
            An[i-1]=0
print(ret)

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

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

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

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

A= list(map(int,input().split()))
B=list(map(int,input().split()))
C=[]
D=[]
count=0
j=A[0]
for i in range(N):

    if A[i]>=B[i]:
        A[i]=A[i]-B[i]
        count+=B[i]

    else:
        if A[i+1]+A[i]>=B[i]:
            A[i+1]=A[i+1]-(B[i]-A[i])
            A[i]=0
            count+=B[i]
        else:

            count+=A[i]+A[i+1]
            A[i + 1] = 0
            A[i] = 0

print(count)

Solution (Python):
import sys, re, os
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 S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

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

        else:
            A[i+1] = A[i+1] - B[i]
            B[i] = 0
    


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

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

sum_beat = 0

for i in range(n):
    if a_list[i] + a_list[i + 1] <= b_list[i]:
        sum_beat += a_list[i] + a_list[i + 1]
        a_list[i + 1] = 0
    else:
        sum_beat += b_list[i]
        a_list[i + 1] = a_list[i + 1] - max(b_list[i] - a_list[i], 0)

print(sum_beat)

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

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

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

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

cnt = 0

for i in range(len(A)-1):
    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):
n = int(input())

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

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

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

_ans = 0
_buff = 0

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

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

print(ans)

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

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

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

p = 0
ans = 0

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

if A[N] <= p:
    ans += A[N]
else:
    ans += p

print(ans)

Solution (Python):
n = int(input())
monsters = list(map(lambda x : int(x), input().split()))
heros = list(map(lambda x : int(x), input().split()))

monster_point = 0
hero_point = 0

ans = 0

for i in range(n):
    monster1 = monsters[i]
    monster2 = monsters[i+1]
    hero1 = heros[i]
    
    if monster1 >= hero1:
        ans += hero1
    elif hero1 <= (monster1 + monster2):
        ans += hero1
        monsters[i+1] -= (hero1 - monster1)
    else:
        ans += (monster1 + monster2)
        monsters[i+1] = 0

print(ans)

Solution (Python):

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

score = 0

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


print(score)

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

Solution (Python):
from sys import stdin

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

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

input = lambda: stdin.readline()
main()

Solution (Python):
import numpy as np
n = int(input())

A = [int(s) for s in input().split()]
B = [int(t) for t in input().split()]
B.append(0)

A = np.array(A)
B = np.array(B)
C = B - A
dm_sum = 0

y = 0
for i in range(n+1):
    dm = C[i] + y
    if dm >= 0:
        dm_sum += A[i]
        if y >= A[i]:
            y = B[i]
        else:
            y = dm
    else:
        dm_sum += B[i] + y
        y = 0

print(dm_sum)

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

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

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

print(ans)

Solution (Python):


def submit():
    n = int(input())
    alist = list(map(int, input().split()))
    blist = list(map(int, input().split()))


    
    a = alist.copy()
    b = blist.copy()
    score_a = 0
    for i in range(0, n):
        if a[i] < b[i]:
            score_a += a[i]
            b[i] -= a[i]
            a[i] = 0
        else:
            score_a += b[i]
            a[i] -= b[i]
            b[i] = 0

        if a[i + 1] < b[i]:
            score_a += a[i + 1]
            b[i] -= a[i + 1]
            a[i + 1] = 0
        else:
            score_a += b[i]
            a[i + 1] -= b[i]
            b[i] = 0


    print(score_a)


if __name__ == '__main__':
    submit()

Solution (Python):
n = int(input())
a_s = list(map(int, input().split()))
b_s = list(map(int, input().split()))

count = 0
next_b1_minus = 0
for a, b1, b2 in zip(a_s, [0]+b_s, b_s+[0]):
    b1 -= next_b1_minus
    if b1 >= a:
        count += a
        next_b1_minus = 0
    elif b1+b2 >= a:
        count += a
        next_b1_minus = a-b1
    else:
        count += b1+b2
        next_b1_minus = b2

print(count)

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

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

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


tmp_ans=0

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



        if(A[i+1]<=B[i]):
            tmp_ans += A[i+1]
            B[i] = B[i]-A[i+1]
            A[i+1] = 0

        
        else:
            tmp_ans += B[i]
            A[i+1] -= B[i] 
            B[i] = 0



    else:
        tmp_ans += B[i]
        A[i] -= B[i]
        B[i] = 0


print(tmp_ans)

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

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

cnt = 0
for i, b in enumerate(BS):
    a = AS[i]
    if a <= b:
        cnt += a
        s = b - a
        if s >= AS[i + 1]:
            cnt += AS[i + 1]
            AS[i + 1] = 0
        else:
            cnt += s
            AS[i + 1] -= s
    else:
        cnt += b


print(cnt)

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

count = 0
i = N
j = N - 1
tmpA = A[i]
tmpB = B[j]
while (i >= 0 and j >= 0):
    if tmpA - tmpB > 0:
        tmpA -= tmpB
        count += tmpB
        j -= 1
        if j >= 0:
            tmpB = B[j]
        if i - j > 1:
            i -= 1
            if i >= 0:
                tmpA = A[i]
    elif tmpB - tmpA > 0:
        tmpB -= tmpA
        count += tmpA
        i -= 1
        if i >= 0:
            tmpA = A[i]
        if i - j < 0:
            j -= 1
            if j >= 0:
                tmpB = B[j]
    else:
        count += tmpA
        i -= 1
        j -= 1
        if i >= 0 and j >= 0:
            tmpA = A[i]
            tmpB = B[j]

print(count)

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

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

print(Nhunt)

Solution (Python):
import numpy as np
N = int(input())
A = np.array(input().split()).astype(type(N))
B = np.array(input().split()).astype(type(N))

Knock_Down = 0

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

    elif(A[i] < B[i]):
        Knock_Down += A[i]
        B[i] -= A[i]
        A[i] = 0
        
        if(A[i + 1] < B[i]):
            
            Knock_Down += A[i + 1]
            B[i] -= A[i+1]
            A[i + 1] = 0
        elif(A[i + 1] >= B[i]):
            Knock_Down += B[i]
            A[i + 1] -= B[i]
            B[i] = 0

print(Knock_Down)

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

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

print(ans)

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
B+=[0]
count=0
power0=0
for n in range(N+1):
    enemy=A[n]
    power1=B[n]
    if power0+power1>=enemy:
        count+=enemy
        power0=power1-max(enemy-power0,0)
    else:
        count+=power0+power1
        power0=0
print(count)

Solution (Python):
n = int(input())
an = []
an = [int(x) for x in input().split()]
bn = []
bn = [int(x) for x in input().split()]

sum = 0
rem = an[n]
for i in range(0, n):
    j = (n - 1) - i
    if rem + an[j] <= bn[j]:
        sum += rem + an[j]
        rem = 0
    else:
        sum += bn[j]
        rem = rem + an[j] - bn[j]
        if rem > an[j]:
            rem = an[j]

print(sum)

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

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

print(r)

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


ans=0
i=0

while i!=n:
    s=[a[i],b[i]]
    a[i]=a[i]-min(s)
    b[i]=b[i]-min(s)
    ans+=min(s)

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

Solution (Python):

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

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

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

r+=min(a[i+1],k)

print r

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, b in enumerate(B):
    k = min(A[i], b)
    ans += k
    A[i] -= k
    b -= k

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

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

print(cnt)

Solution (Python):
def resolve():
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    aa.reverse()
    bb.reverse()
    bb.append(0)

    cnt = 0
    zan = 0
    for a, b in zip(aa, bb):
        
        cnt += min(a, zan)
        
        if a > zan:
            cnt += min(a - min(a, zan), b)
            zan = b - min(a - min(a, zan), b)
        else:
            zan = b

    print(cnt)
    
resolve()

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

for i in range(N):
  if list_a[i] <= list_b[i]:
    ans += list_a[i]
    list_b[i] -= list_a[i]
    list_a[i] = 0
    
    if list_a[i+1] <= list_b[i]:
      ans += list_a[i+1]
      list_a[i+1] = 0
    else:
      ans += list_b[i]
      list_a[i+1] -= list_b[i]
  else:
    ans += list_b[i]

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
monst = 0
C = A[N]
R = B[N-1] - C
R = max(R,0)
monst += B[N-1] - R
if N > 1:
    for i in range(N-1,0,-1):
        C = A[i]
        Q = R - C
        Q = max(Q,0)
        monst += R - Q
        P = max(0,C-R+Q)
        R = B[i-1] - P
        R = max(R,0)
        monst += B[i-1] - R
    monst += min(A[0],R)
print(monst)

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

Solution (Python):
import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)

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

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

Solution (Python):
def main():
	import sys
	input = sys.stdin.readline
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	ans = 0
	rest = A[0]
	for i in range(N):
		if rest >= B[i]:
			ans += B[i]
			rest = A[i+1]
		elif (rest < B[i]) & (rest + A[i+1] >= B[i]):
			ans += B[i]
			rest = rest + A[i+1] - B[i]
		else:
			ans += (rest + A[i+1])
			rest = 0
	print(ans)

if __name__ == '__main__':
    main()

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

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

fin = sum(a)  
print(init-fin)

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

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

AList = list(map(int, input().split()))
BList = list(map(int, input().split()))

ans = 0

for i in range(N):
    ans += min(AList[i], BList[i])
    BList[i] = BList[i] - min(AList[i], BList[i])

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

print(ans)

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

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