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

killed = 0

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

print(killed)

Solution (Python):
N = int(input())
A_l = list(map(int,input().split()))
A_all = sum(A_l)
B_l = list(map(int,input().split()))

for i in range(N):
  a1 = A_l[i]
  a2 = A_l[i+1]
  b1 = B_l[i]
  if b1>a1:
    A_l[i] = 0
    b1 -= a1
  else:
    A_l[i] = a1-b1
    continue
  if b1>a2:
    A_l[i+1] = 0
  else:
    A_l[i+1] = a2-b1

print(A_all - sum(A_l))

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

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    b = list(map(int, input().rstrip('\n').split()))
    cnt = 0
    for i in range(n):
        s = min(a[i], b[i])
        a[i] -= s
        b[i] -= s
        cnt += s

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


if __name__ == '__main__':
    slove()

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):
    tmp = min(a[i], b[i])
    ans += tmp
    a[i] -= tmp
    tmp2 = min(a[i+1], b[i] - tmp)
    ans += tmp2
    a[i+1] -= tmp2
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 B[i] >= A[i] + A[i+1]:
        cnt += A[i] + A[i+1]
        A[i+1] = 0
    elif A[i] < B[i]:
        cnt += A[i] + (B[i] - A[i])
        A[i+1] =  A[i+1] - (B[i] - A[i])
    elif A[i] > B[i]:
        cnt += B[i]
    elif A[i] == B[i]:
        cnt += A[i]
print(cnt)

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

count = 0
for i in range(n):
    diff = a[i] - b[i]
    if diff >= 0:
        count += b[i]
    else:
        count += a[i]
        _diff = a[i+1] + diff
        if _diff >= 0:
            count += abs(diff)
            a[i+1] = _diff
        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()))
count = 0
for i in range(n):
    if a[i] >= b[i]:
        count += b[i]
    else:
        count += a[i]
        b[i] -= a[i]
        if a[i+1] >= b[i]:
            count += b[i]
            a[i+1] -= 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()))
first = 0
second = 0
count = 0
for i in range(0, N):
    first = min(A[i] - second, B[i])
    second = min(A[i + 1], B[i] - first)
    
    count += first + second

print(count)

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

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]
        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]
    else:
        ans += B[i]
        A[i] -= 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): 
    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]
            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):
n=int(input())
ml=list(map(int,input().split()))
sl=list(map(int,input().split()))
ans,a1=0,ml[0]
for a2,b in zip(ml[1:],sl):
  x=min([a1,b])
  y=b-min([a1,b])
  z=min([y,a2])
  a1=a2-z
  ans+=(x+z)
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):
    for j in range(2):
        if b[i] - a[i+j] <= 0:
            a[i+j] -= b[i]
            ans += b[i]
            break
        else:
            b[i] -= a[i+j]
            ans += a[i+j]
            a[i+j] = 0

print(ans)

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

C = [0] * (N+1)

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

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

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

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

bl = [0] + bl
mod = 0
kill = 0
for i in reversed(range(n+1)):
	if ml[i] >= bl[i] + mod:
		kill += bl[i] + mod
		mod = 0
	else:
		kill += ml[i]
		if mod >= ml[i]:
			mod = bl[i]
		else:
			mod = bl[i] + mod  - ml[i] 

print(kill)

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

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

ans=0
for i in range(n):
  c=As[i]+As[i+1]
  if c>Bs[i]:
    ans+=Bs[i]
    if Bs[i]>As[i]:
      As[i+1]-=Bs[i]-As[i]
  else:
    ans+=c
    As[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):
  c = a[i] + a[i+1]
  if c > b[i]:
    ans += b[i]
    if a[i] > b[i]:
      continue
    else:
      a[i+1] -= b[i] - a[i]
  else:
    ans += c
    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):
    if A[i]>=B[i]:
        ans += B[i]
    else:
        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]
print(ans)

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

dp = [0]*(N+1)
Rm = [0]*(N+2)
dp[1] = min(A[1]+A[2],B[1])
Rm[1] = A[1]

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

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

Solution (Python):
def solver(A,B):
    
    A_before=sum(A)
    
    
    for target in range(n+1):
    
        
        if target>0 and B[target-1]>0:
            if A[target]<=B[target-1]:
                B[target-1]=B[target-1]-A[target]
                A[target]=0
                continue 
            else:
                A[target]=A[target]-B[target-1]
                B[target-1]=0
                
                
        if target < n:        
            if A[target] >= B[target]:
                A[target]=A[target]-B[target]
                B[target]=0
                continue
            else:
                B[target]=B[target]-A[target]
                A[target]=0
                
                
            
    A_after=sum(A)
    
    return A_before-A_after
    
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
print(solver(A,B))

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

Solution (Python):
import math
import sys
import numpy as np
from collections import defaultdict, deque, Counter 
from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd

max_value = 1000000000
min_value = -1000000000
sys.setrecursionlimit(max_value)

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

v = 0
for i in range(N):
    if As[i] >= Bs[i]:
        v += Bs[i]
        As[i] -= Bs[i]
        Bs[i] = 0
        continue
    else:
        v += As[i]
        Bs[i] -= As[i]
        As[i] = 0
        if As[i+1] >= Bs[i]:
            v += Bs[i]
            As[i+1] -= Bs[i]
            Bs[i] = 0
        else:
            v += As[i+1]
            Bs[i] -= As[i+1]
            As[i+1] = 0
print(v)

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 A[i] >= B[i]:
    c += B[i]
  else:
    c += A[i]
    B[i] -= 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()))

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

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

before = sum(A)

for  n in range(N):
    t = min(A[n],B[n])
    A[n] -= t
    B[n] -= t
    A[n+1] -= min(A[n+1],B[n])
    
after = sum(A)
print(before-after)

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

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

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


x=0
c=0

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

if a[N]<x:
  x=a[N]
else:
  x=max(x-a[N],0)

print(c)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = min(a[0], b[0])
tmp = max(0, b[0]-a[0])

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

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

total_monster = 0
for i in range(n):
    monster = min(a[i], b[i])
    total_monster += monster
    a[i] -= monster
    b[i] -= monster
    monster = min(a[i + 1], b[i])
    total_monster += monster
    a[i + 1] -= monster
print(total_monster)

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

remained_enemies_sum = sum(A)
killed_enemies_sum = enemy_sum - remained_enemies_sum
print(killed_enemies_sum)

Solution (Python):


SELECT = "13" + "567"[0] + "ABC"[2]


if SELECT == "135A":  

    a, b = map(int, input().split())  
    c = abs(a - b)  
    if c % 2 == 0:  
        print(c // 2 + min((a, b)))  
    else:
        print("IMPOSSIBLE")


if SELECT == "135B":  

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

    
    count = 0
    for i in range(len(nums)):
        if nums[i] != (i + 1):
            count += 1
    if count < 3:
        print("YES")
    else:
        print("NO")


if SELECT == "135C":  

    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()))
ans = 0
for i in range(n):
	rem = max(B[i]-A[i], 0)
	ans += min(A[i], B[i]) +  min(rem, A[i+1])
	A[i+1] = max(A[i+1]- rem, 0)
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = min(a[0], b[0])
tmp = max(0, b[0]-a[0])

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

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

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

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

Solution (Python):


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

NumMonsBegin = sum(mons)

for i in range(0,n):
    if mons[i] >= hero[i]:
        mons[i] = mons[i]-hero[i]


    else:
        yoryoku = hero[i]-mons[i]
        mons[i] = 0
        mons[i+1] = mons[i+1] - yoryoku
        if mons[i+1] < 0:
            mons[i+1] = 0

NumMonsLeft = sum(mons)

print(NumMonsBegin - NumMonsLeft)

Solution (Python):
import sys
from copy import deepcopy
from collections import deque
sys.setrecursionlimit(20000000)


def main():
    N = int(input())
    monsters = list(map(int, input().split()))
    heroes = list(map(int, input().split()))

    defeated = 0

    for i in reversed(range(len(heroes))):
        if heroes[i] == 0:
            continue

        for _ in [1, 0]:
            if monsters[i+_] >= heroes[i]:
                defeated += heroes[i]
                monsters[i+_] -= heroes[i]
                heroes[i] = 0
            elif monsters[i+_] < heroes[i]:
                defeated += monsters[i+_]
                heroes[i] -= monsters[i+_]
                monsters[i+_] = 0

    print(defeated)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
c=0
for i in range(n):
  if l1[i]>l2[i]:
    c+=l2[i]
    l2[i]=0
  elif l1[i]==l2[i]:
    c+=l1[i]
    l2[i]=0
  else:
    c+=l1[i]
    l2[i]=l2[i]-l1[i]
    if l2[i]>=l1[i+1]:
      c+=l1[i+1]
      l1[i+1]=0
    elif l2[i]<l1[i+1]:
      c+=l2[i]
      l1[i+1]=l1[i+1]-l2[i]
print(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):
    c = min(A[i], B[i])
    B[i] -= c
    A[i] -= c
    total += c
    c = min(A[i + 1], B[i])
    B[i] -= c
    A[i + 1] -= c
    total += c
print(total)

Solution (Python):
N=int(input())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))

ans=0
for i in range(N):
  if l1[i]>=l2[i]:
    ans+=l2[i]
  elif l1[i+1]+l1[i]>=l2[i]:
    l1[i+1]=l1[i+1]-(l2[i]-l1[i])
    ans+=l2[i]
  else:
    ans+=l1[i]+l1[i+1]
    l1[i+1]=0
print(ans)

Solution (Python):


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
beat = []
remain = []
 
for i in range(n+1):
  if i==0:
    if a[i]<=b[i]:
      beat.append(a[i])
      remain.append(0)
      b[i]-=a[i]
    else:
      beat.append(b[i])
      remain.append(a[i]-b[i])
      b[i] = 0
  elif i==n:
    if a[i]<=b[i-1]:
      beat.append(a[i])
      remain.append(0)
      b[i-1]-=a[i]
    else:
      beat.append(b[i-1])
      remain.append(a[i]-b[i-1])
      b[i-1] = 0
  elif a[i]<=b[i-1]:
    b[i-1] -= a[i]
    beat.append(a[i])
    remain.append(0)
  elif a[i] > b[i-1] and a[i]<=b[i]+b[i-1]:
    b[i] -= a[i]-b[i-1]
    b[i-1] = 0
    beat.append(a[i])
    remain.append(0)
  else:
    beat.append(b[i-1]+b[i])
    remain.append(a[i]-b[i-1]-b[i]) 
    b[i]=0
    b[i-1]=0
print(sum(beat))

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


at = 0
over = 0
for i in range(n):
    diff = max(0, a[i] - over) - b[i]
    over = max(0, -diff)
    at += max(0, diff)
print(sum(a) - (at + max(0, a[-1] - over)))

Solution (Python):
n=int(input())
a_list=[int(s) for s in input().split()]
b_list=[int(s) for s in input().split()]

total=0
for i,b in enumerate(b_list):
    a1=a_list[i]
    
    enemy=min(a1,b)
    if a1>=0 and b>0:
        b=b-enemy
        total+=enemy
    
    if b<=0:
        continue
    
    a2=a_list[i+1]
    enemy=min(a2,b)
    total+=enemy
    a_list[i+1]-=enemy
print(total)

Solution (Python):
n = int(input())
list_a = list(map(int, input().split()))
list_b = list(map(int, input().split()))

res = 0
for i in range(n):
    a = list_a[i]
    b = list_b[i]
    if b >= a:
        res += a
        b -= a
    else:
        res += b
        b = 0
    c = list_a[i + 1]
    if b >= c:
        list_a[i + 1] = 0
        res += c
    else:
        list_a[i + 1] -= b
        res += b
print(res)

Solution (Python):


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
beat = []
remain = []

for i in range(n+1):
  if i==0:
    if a[i]<=b[i]:
      beat.append(a[i])
      remain.append(0)
      b[i]-=a[i]
    else:
      beat.append(b[i])
      remain.append(a[i]-b[i])
      b[i] = 0
  elif i==n:
    if a[i]<=b[i-1]:
      beat.append(a[i])
      remain.append(0)
      b[i-1]-=a[i]
    else:
      beat.append(b[i-1])
      remain.append(a[i]-b[i-1])
      b[i-1] = 0
  elif a[i]<=b[i-1]:
    b[i-1] -= a[i]
    beat.append(a[i])
    remain.append(0)
  elif a[i] > b[i-1] and a[i]<=b[i]+b[i-1]:
    b[i] -= a[i]-b[i-1]
    b[i-1] = 0
    beat.append(a[i])
    remain.append(0)
  else:
    beat.append(b[i-1]+b[i])
    remain.append(a[i]-b[i-1]-b[i]) 
    b[i]=0
    b[i-1]=0

print(sum(beat))

Solution (Python):
import math
import copy
import sys
import numpy as np



def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputaNum]
        outputDataList.append(inputDataList)

    return outputDataList



n = getInputNum()

a = getInputListInt()
b = getInputListInt()

b.append(0)

ans = 0
tmp = 0

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

print(ans)

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



result=0

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

print(result)

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

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

total_monster = sum(A)

if A[0] >= B[0]:  
    A[0] -= B[0]
    B[0] = 0
else:  
    B[0] -= A[0]
    A[0] = 0

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

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


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

tmp = sum(A)

print(total_monster - tmp)

Solution (Python):
N = int(input())
M = list(map(int, input().split()))
U = list(map(int, input().split()))

ans = 0
for i in range(N):
    m = M[i] + M[i + 1] - U[i]
    if m > 0:
        ans += U[i]
        M[i + 1] = min(m, M[i + 1])
    else:
        ans += M[i] + M[i + 1]
        M[i + 1] = 0

print(ans)

Solution (Python):


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

r = 0
k = 0

for i in range(n):
    if r >= 0:
        m = min(r + b[i], a[i])
        k += m
        re = b[i] - max(0, m - r)
        r = re

    else:
        m = min(b[i], a[i])
        k += m
        r = b[i] - m

if r > 0:
    k += min(r, a[n])


print(k)

Solution (Python):
n = int(input())
l_a = list(map(int, input().split()))
l_b = list(map(int, input().split()))

ans = 0
for a, _ in enumerate(l_b):
  if l_a[a] < l_b[a] :
    if l_a[a+1] <= l_b[a] - l_a[a]:
      ans += l_a[a] + l_a[a+1]
      l_a[a+1] = 0
    else:
      ans += l_a[a] + l_b[a] - l_a[a]
      l_a[a+1] -= l_b[a] - l_a[a]
  else:
    ans += l_b[a]
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):
  if a[i] >= b[i]:
    c += b[i]
    
  elif a[i] < b[i]:
    c += a[i]
    d = a[i+1]
    a[i+1] = a[i+1] + a[i] - b[i]
    if a[i+1] < 0:
      a[i+1] = 0
      c += d
    else:
      c += (b[i] - a[i])
    
print(c)

Solution (Python):
N = int(input())
monsters = list(map(int, input().split()))
heros = list(map(int, input().split()))
nums = 0

for i in range(len(heros)):
    if monsters[i] <= heros[i]:
        heros[i] -= monsters[i]
        nums += monsters[i]
        monsters[i] = 0

        if monsters[i+1] <= heros[i]:
            heros[i] -= monsters[i+1]
            nums += monsters[i+1]
            monsters[i+1] = 0
        else:
            nums += heros[i]
            monsters[i+1] -= heros[i]
            heros[i] = 0
    else:
        nums += heros[i]
        monsters[i] -= heros[i]
        heros[i] = 0

print(nums)

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

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

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

    elif A[i] - B[i] <= 0:
        output += B[i]
        A[i], A[i + 1] = 0, A[i + 1] - (B[i] - A[i])

    else:
        output += B[i]
        A[i] -= B[i]

print(output)

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

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

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

    if B[n] > 0:
        if A[n+1] >= B[n]:
            A[n+1] = A[n+1] - B[n]
            cnt = cnt + B[n]
        else:
            cnt = cnt + A[n+1]
            A[n+1] = 0


print(cnt)

Solution (Python):
n = int(input())
li_A = list(map(int,input().split()))
li_B = list(map(int,input().split()))
cnt = 0
for i in range(0,n):
    defeat = min(li_A[i],li_B[i])
    cnt += defeat
    li_B[i] -= defeat
    re_defeat = min(li_A[i+1],li_B[i])
    cnt+=re_defeat
    li_A[i+1] -=re_defeat
print(cnt)

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

count = 0
x = 0  
for i in range(N):
    a = A[i]
    b = B[i]
    
    if x < a:  
        count += x
        a = a - x  

        if a <= b:  
            count += a
            
            x = b - a
        else:
            count += b
            x = 0
    else:  
        count += a
        x = b
    



if A[N] >= x:
    count += x
else:
    count += A[N]
print(count)

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

sum = 0
for i in range(N):
  if A[i] >= B[i]:
    sum += B[i]
  elif A[i] < B[i]:
    sum += A[i]
    C = B[i] - A[i]
    if A[i+1] >= C:
      sum += C
      A[i+1] = A[i+1] - C
    elif A[i+1] < C:
      sum += 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()))
cnt=0
rem=0
for i in range(n):
  if a[i]>=rem:
    cnt+=rem
    a[i]=a[i]-rem
  else:
    cnt+=a[i]
    rem=b[i]
    continue
  if a[i]>=b[i]:
    cnt+=b[i]
    rem=0
  else:
    cnt+=a[i]
    rem=b[i]-a[i]
if a[n]>=rem:
  cnt+=rem
else:
  cnt+=a[n]
print(cnt)

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

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

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