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

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

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

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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
count=0
for i in range(N):
  count +=min(A[i],B[i])
  if A[i]>=B[i]:
    B[i]=0
  else:
    B[i] -= A[i]
  count +=min(A[i+1],B[i])
  if A[i+1]<=B[i]:
    A[i+1]=0
  else:
    A[i+1] -=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):
    cankill = B[i]
    ans += min([A[i],B[i]])
    if B[i] - A[i] > 0:
        additional = min([B[i] - A[i],A[i+1]])
        ans += additional
        A[i+1] += - additional
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):
    left = min(a[i], b[i])
    ans += left
    right = min(a[i + 1], b[i] - left)
    ans +=  right
    a[i + 1] -= right

print(ans)

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

print(ans)

Solution (Python):

import sys
input = sys.stdin.readline


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

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

            if b[i] > a[i]:
                result += a[i]
                b[i] = b[i] - a[i]
                a[i] = 0
            else:
                result += b[i]
                a[i] = a[i] - b[i]
                b[i] = 0
        else:
            result += b[i]
            a[i + 1] = a[i + 1] - b[i]
            b[i] = 0
            continue

    print(result)


if __name__ == '__main__':
    main()

Solution (Python):

import sys










def main():
    n = int(sys.stdin.readline().rstrip())
    a = [int(x) for x in sys.stdin.readline().split()]
    b = [int(x) for x in sys.stdin.readline().split()]
    at_beginning = 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

    at_ending = sum(a)

    ans = at_beginning - at_ending
    print(ans)
   

if __name__ == "__main__":
    main()

Solution (Python):
ii = lambda : int(input())
li = lambda : list(map(int, input().split()))

n = ii()
a = li()
b = li()

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

Solution (Python):
from collections import defaultdict,deque, Counter
import sys,heapq,bisect,math,itertools,string,queue,copy,time
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n = inp()
Alist = inpl()
Blist = inpl()
ans = 0
for i in range(n):
    ApA = Alist[i]+Alist[i+1]
    if Blist[i] >= ApA:
        ans += ApA
        Alist[i+1] = 0
    elif Blist[i] >= Alist[i] and Blist[i] <ApA:
        ans += Blist[i]
        Alist[i+1] = ApA - Blist[i]
    elif Blist[i] < Alist[i]:
        ans += Blist[i]

print(ans)

Solution (Python):

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

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

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
def print_ans(N, A, B):
    """Test Case
    >>> print_ans(2, "3 5 2", "4 5")
    9
    >>> print_ans(3, "5 6 3 8", "5 100 8")
    22
    >>> print_ans(2, "100 1 1", "1 100")
    3
    """
    ar = list(map(int, A.split()))
    br = list(map(int, B.split()))
    ans = 0
    for i in range(N):
        if br[i] < ar[i]:
            ans += br[i]
        else:
            ans += ar[i]
            br[i] -= ar[i]
            if ar[i + 1] <= br[i]:
                ans += ar[i + 1]
                ar[i + 1] = 0
            else:
                ans += br[i]
                ar[i + 1] -= br[i]  
    print(ans)

if __name__ == '__main__':
    N= int(input().rstrip())
    A = input().rstrip()
    B = input().rstrip()    
    print_ans(N, A, B)

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

if a[0] + a[1] - b[0] >= 0:
    count += b[0]
    if a[0] <= b[0]:
        rest = a[0] + a[1] - b[0]
    else:
        rest = a[1]    
else:
    count += a[0] + a[1]


for i in range(1,n):
    han = rest + a[i+1] - b[i]
    if han >= 0:
        count += b[i]
        if rest <= b[i]:
            rest = han
        else:
            rest = a[i+1]
    else:
        count += rest + a[i+1]
        rest = 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 = count + A[i]
		B[i] = B[i] - A[i]
		A[i] = 0
	else:
		count = count + B[i]
		A[i] = A[i] - B[i]
		B[i] = 0
	
	if A[i + 1] < B[i]:
		count = count + A[i + 1]
		B[i] = B[i] - A[i + 1]
		A[i + 1] = 0
	else:
		count = count + B[i]
		A[i + 1] = A[i + 1] - B[i]
		B[i] = 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):
    ans += min(B[i], A[i]+A[i+1])
    A[i+1] = max(0, A[i+1] - max(0, (B[i]-A[i])))
print(ans)

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

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

count = 0
for i in range(N):
    if B[i] <= A[i]:
        count += B[i]
    else:
        count += A[i] + np.minimum((B[i] - A[i]), A[i+1])
        A[i+1] -= np.minimum((B[i] - A[i]), A[i+1])
print(count)

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

Solution (Python):


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

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

print(ans)

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

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

for i in range(n):
    for j in range(i,i+2):
        c = b[i] - a[j]
        if c <= 0:
            a[j] -= b[i]
            s += b[i]
            break
        else:
            b[i] -= a[j]
            s += a[j]
            a[j] = 0

print(s)

Solution (Python):
N = int(input())
Monsters = [int(i) for i in input().split()]
Heroes = [int(i) for i in input().split()]
res = 0
for i in range(N):
    r = Monsters[i] - Heroes[i]
    if r >= 0:
        Monsters[i] -= Heroes[i]
        res += Heroes[i]
    else:
        res += Monsters[i]
        Monsters[i] = 0
        k = Monsters[i+1] + r
        if k >= 0:
            res -= r
            Monsters[i+1] += r
        else:
            res += Monsters[i+1]
            Monsters[i+1] = 0

print(res)

Solution (Python):


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



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

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b



def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2



def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)



def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

    total = sum(A)
    for i in range(N):
        r = min(A[i], B[i])
        A[i] -= r
        B[i] -= r

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

    print(total - sum(A))

if __name__ == '__main__':
    main()

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

count = 0

hero_old = 0

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

print(count)

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

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

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

print(l)

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

total = 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]
    else:
        a[i] = a[i] - b[i]

kill = total - sum(a)

print(kill)

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

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

for i, bi in enumerate(reversed(b)):
    id = n-i
    if a[id] >= bi:
        ans += bi
    else:
        ans += a[id]
        bi -= a[id]
        id -= 1
        if a[id] >= bi:
            ans += bi
            a[id] -= bi
        else:
            ans += a[id]
            a[id] = 0

print(ans)

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

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

for i in range(n):

  diff = min(a[i], b[i])
  cnt += diff
  a[i] -= diff
  b[i] -= diff

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

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))
    score=0
    
    for i in range(n):
        k=min(A[i],B[i])
        score+=k
        A[i]-=k
        B[i]-=k
        if(B[i]>0):
            k=min(A[i+1],B[i])
            score+=k
            A[i+1]-=k
            B[i]-=k
    print(score)
resolve()

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+1] > (B[i] - A[i]):
        A[i+1] -= B[i] - A[i]
        ans += B[i]
    else:
        ans += A[i] + A[i+1]
        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()))
bf = sum(A_list)

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

af = sum(A_list)
print(bf- af)

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

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


print(ans)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
total = 0
zan = 0
for i in range(N):
  dif = B[i] + zan - A[i]
  total += min( B[i] + zan, A[i] )
  zan = min( max(0,dif), B[i])
total += min(zan, A[N] )
print(total)

Solution (Python):
N = int(input())
AList = list(map(int, input().split()))
BList = list(map(int, input().split()))

enemy = 0
for i in range(N):
    
    a = min([AList[i], BList[i]])
    
    b = min([AList[i + 1], max([BList[i] - AList[i], 0])])
    AList[i + 1] = max([AList[i + 1] - b, 0])
    
    enemy = enemy + a + b
print(enemy)

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

count=0
for i in range(n):
      count+=min(A[i],B[i])
      if A[i]<B[i]:
            pre=A[i+1]
            A[i+1]=max(0,A[i+1]-(B[i]-A[i]))
            count+=pre-A[i+1]
print(count)

Solution (Python):




n=int(input())
al = list(map(int,input().split()))
bl = list(map(int,input().split()))

ans = 0
rp = 0
for idx, a in enumerate(al):
    if idx == n:
        ans += min(rp, a)
    else:
        b = bl[idx]
        enemy = min(a,b+rp)
        ans += enemy
        rp = min(max(b - enemy + rp, 0), b)
print(ans)

Solution (Python):
N = int( input() )
A_list = list( map( int, input().split() ) )
B_list = list( map( int, input().split() ) )
c = min(A_list[0], B_list[0])
remain_B = max(B_list[0] - A_list[0], 0)
for i in range(1, N ):
    c += min(remain_B, A_list[i])
    remain_A = max( A_list[i] - remain_B, 0 )
    c += min(remain_A, B_list[i])
    remain_B = max(B_list[i] - remain_A, 0 )
c += min(A_list[N], remain_B)
print(c)

Solution (Python):
n = int(input().strip())
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()))
cnt=0
for i in range(N):
  rem=B[i]-A[i]
  if rem>0:
    cnt+=(A[i]+min(rem,A[i+1]))
    A[i+1]-=rem
    if A[i+1]<0:
      A[i+1]=0
  else:
    cnt+=B[i]
    
print(cnt)

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

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

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

Solution (Python):
n=int(input())
monsters=[int(i) for i in input().split()]
powers=[[int(i),0] for i in input().split()]
monsters.append(0)
powers.extend([[0,0],[0,0]])

breaks=[]
for i in range(n+1):
  if monsters[i]>powers[i][1]:
    attack=sum(powers[i])
    if monsters[i]>=attack:
      breaks.append(attack)
    else:
      breaks.append(monsters[i])
      powers[i+1][1]=attack-monsters[i]
                    
  else:
    powers[i+1][1]=powers[i][0]
    breaks.append(monsters[i])
    
print(sum(breaks))

Solution (Python):
n=int(input())
monsters=[int(i) for i in input().split()]
powers=[[int(i),0] for i in input().split()]
monsters.append(0)
powers.extend([[0,0],[0,0]])

breaks=[]
for i in range(n+1):
  if monsters[i]>powers[i][1]:
    
    if monsters[i]>=sum(powers[i]):
      breaks.append(sum(powers[i]))
    else:
      breaks.append(monsters[i])
      powers[i+1][1]=sum(powers[i])-monsters[i]
                    
  else:
    powers[i+1][1]=powers[i][0]
    breaks.append(monsters[i])
    
print(sum(breaks))

Solution (Python):
n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
m = 0
cnt = 0
for i, a in enumerate(a_list):
    b = 0 if i == n else b_list[i]
    cnt += min(m+b, a)
    m = max(0, b - max(0, a-m))
print(cnt)

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


ans = 0
Ai = A[0]
Bi = B[0]

for i in range(N):
    
    ans += min(Ai, Bi)

    
    if Ai < Bi:
        ans += min(A[i + 1], Bi - Ai)
        Ai = A[i + 1] - min(A[i + 1], Bi - Ai)
    else:
        Ai = A[i + 1]

    
    if i == (N - 1):
        break
    else:
        Bi = B[i + 1]

print(ans)

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

Solution (Python):
n = int(input())
an = list(map(int, input().split()))
bn = list(map(int, input().split()))



bsum = sum(bn)
for i in range(n):
    diff = an[i] - bn[i]
    if diff <= 0:
        diff = bn[i] - an[i]
        an[i] = 0
        bn[i] = diff
    else:
        an[i] = diff
        bn[i] = 0
    
    diff2 = an[i+1] - bn[i]
    if diff2 <= 0:
        diff2 = bn[i] - an[i+1]
        an[i+1] = 0
        bn[i] = diff2
    else:
        an[i+1] = diff2
        bn[i] = 0
ans = bsum - sum(bn)
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
tmp_sum = sum(a)
a = list(reversed(a))
b = list(reversed(b))
for i in range(len(b)):
    tmp = b[i] - a[i]
    a[i] = max(a[i] - b[i], 0)
    a[i+1] = max(a[i+1] - max(tmp, 0),0)
res_sum = sum(a)
print(tmp_sum - res_sum)

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

sum_ = 0

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

Solution (Python):
import unittest


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think([3, 5, 2], [4, 5]), 9)

    def test_2(self):
        self.assertEqual(think([5, 6, 3, 8], [5, 100, 8]), 22)

    def test_3(self):
        self.assertEqual(think([100, 1, 1], [1, 100]), 3)


def solve():
    a, b = read()
    result = think(a, b)
    write(result)


def read():
    n = read_int(1)[0]
    a = read_int(n + 1)
    b = read_int(n)
    return a, b


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_float(n):
    return list(map(lambda x: float(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a, b):
    count = 0
    for index, value in enumerate(b):
        if a[index] + a[index + 1] <= value:
            count += a[index] + a[index + 1]
            a[index] = 0
            a[index + 1] = 0
        else:
            if a[index] <= value:
                count += a[index]
                value -= a[index]
                a[index] = 0
                if value > 0:
                    count += value
                    a[index + 1] -= value
            else:
                count += value
                a[index] -= value
    return count


def write(result):
    print(result)


if __name__ == '__main__':
    
    solve()

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

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

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

Solution (Python):
n = int(input())
number_of_monsters =  list(map(int,input().split()))
feasible_beat =  list(map(int,input().split()))

start = sum(number_of_monsters)
for i in range(n):
    if number_of_monsters[i] < feasible_beat[i]:
        number_of_monsters[i+1] -= (feasible_beat[i] - number_of_monsters[i])
        number_of_monsters[i] = 0
        if number_of_monsters[i+1] < 0:
            number_of_monsters[i+1] = 0
    else:
        number_of_monsters[i] -= feasible_beat[i]
print(start-sum(number_of_monsters))

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

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

print(cnt)

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

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

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
total = sum(A)

for i in range(n):
    b = max(B[i] - A[i], 0)
    A[i] = max(A[i]-B[i], 0)
    A[i+1] = max(A[i+1]-b, 0)

print(total-sum(A))

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

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

sumb = sum(b)

for i in range(0, n):
    b[i] = b[i] - a[i]
    if b[i] > 0:
        a[i+1] = a[i+1] - b[i]
        if a[i+1] < 0:
            b[i] = abs(a[i+1])
            a[i+1] = 0
        elif a[i+1] >= 0:
            b[i] = 0
    elif b[i] <= 0:
        b[i] = 0

print(sumb - sum(b))

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

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

Solution (Python):

n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))

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

    if bl[i]>0:
        if al[i+1]<bl[i]: 
            res += al[i+1]
            bl[i] -= al[i+1]
            al[i+1] = 0
        else:
            res += bl[i]
            al[i+1] -= bl[i]
            bl[i] = 0

print(res)

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

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


if __name__ == '__main__':
    main()

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

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

Solution (Python):
N = int(input())
MONSTER = list(map(int, input().split()))
HERO = list(map(int, input().split()))

count = 0

for i in range(N):
    battle = min(HERO[i], MONSTER[i])
    count += battle
    HERO[i] -= battle
    MONSTER[i] -= battle

    if HERO[i] > 0:
        battle = min(HERO[i], MONSTER[i+1])
        count += battle
        HERO[i] -= battle
        MONSTER[i+1] -= battle


print(count)