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())
As = input().split(' ')
As = [int(As[i]) for i in range(N+1)]
Bs = input().split(' ')
Bs = [int(Bs[i]) for i in range(N)]
WIN = 0
for i in range(N):
  if Bs[i] > As[i]:
    WIN += As[i]
    Bs[i] -= As[i]
    As[i] = 0
    if Bs[i] > As[i+1]:
      WIN += As[i+1]
      Bs[i] -= As[i+1]
      As[i+1] = 0
    elif Bs[i] == As[i+1]:
      WIN += As[i+1]
      Bs[i] = 0
      As[i+1] = 0
    else:
      WIN += Bs[i]
      As[i+1] -= Bs[i]
      Bs[i] = 0
  elif Bs[i] == As[i]:
    WIN += As[i]
    Bs[i] = 0
    As[i] = 0
  else:
    WIN += Bs[i]
    As[i] -= Bs[i]
    Bs[i] = 0
print(WIN)

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

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


kill_mon.append(0)


killed_monster = 0
carry_power = 0
for mons, kill in zip(monster_list, kill_mon):   
    
    if mons >= (kill + carry_power):
        killed_monster += (kill + carry_power)
        
        carry_power = 0
    
    else:
        killed_monster += mons
        
        
        
        
        carry_power = kill - max(0, mons - carry_power)
print(killed_monster)

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

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

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

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

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

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

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

if a[-1] < 0:
    a[-1] = 0
print(sum_a - sum(a))

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

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

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

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

if hero>0:
    monster += min(hero,A[N])
print(monster)

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


if B[N-1]-C[N-1] > 0:
  ans = cnt + min(A[N], (B[N-1] - C[N-1]))
else:
  ans = cnt
print(ans)

Solution (Python):









import os
import sys




def main():
    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]
        elif A[i] < B[i] and A[i+1] < B[i]-A[i]:
            ans += A[i+1] + A[i]
            A[i+1] = 0
            A[i] = 0
        elif A[i] < B[i] and A[i+1] >= B[i]-A[i]:
            ans += B[i]
            A[i+1] -= B[i]-A[i]
            A[i] = 0

    print(ans)


if __name__ == "__main__":
    main()

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

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

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

Solution (Python):
from collections import deque
from bisect import bisect_left
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]
    d = B[i] - A[i]
    if d <= A[i+1]:
      cnt += d
      A[i+1] -= d
    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()))

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

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

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

print(ans-sum(B))

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

    for i in range(n):
        c = min(a_list[i], b_list[i])
        ans += c
        d = min(b_list[i] - c, a_list[i + 1])
        ans += d
        a_list[i + 1] -= d

    print(ans)


if __name__ == "__main__":
    main()

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

    for i in range(n):
        if a_list[i] >= b_list[i]:
            ans += b_list[i]
        else:
            d = b_list[i] - a_list[i]
            ans += a_list[i]
            if a_list[i + 1] >= d:
                ans += d
                a_list[i + 1] = a_list[i + 1] - d
            else:
                ans += a_list[i + 1]
                a_list[i + 1] = 0

    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()
    B = LI()

    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)

if __name__ == '__main__':
    resolve()

Solution (Python):
from numpy import cumsum
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, min(b[i] - a[i], a[i+1]))

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

print(c)

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

for n in range(N):
  if A[n]>=B[n]:
    A[n]-=B[n]
  else:
    A[n+1] = max(0,A[n+1]+A[n]-B[n])
    A[n] = 0
    
print(M-sum(A))

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

goukei=sum(a)

for i in range(len(b)):
    if b[i]>a[i]:
        a[i+1]=a[i+1]+a[i]-b[i]
        a[i]=0
        if a[i+1]<0:
            a[i+1]=0
    else:
        a[i]=a[i]-b[i]
print(goukei-sum(a))

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

for n in range(N):
  if A[n]>=B[n]:
    A[n]-=B[n]
  else:
    A[n+1] = max(0,A[n+1]+A[n]-B[n])
    A[n] = 0
    
print(M-sum(A))

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

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

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

print(sum)

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

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy







mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
al = inpl()
bl = inpl()
ans = 0
rem = 0
for i in range(0, n):
    if(al[i] <= bl[i] + rem):
        tmp = al[i] - rem
        if(tmp <= 0):
            tmp = 0
        rem = bl[i] - tmp
        ans += al[i]
    else:
        ans += bl[i] + rem
        rem = 0
if(al[n] >= rem):
    ans += rem
else:
    ans += al[n]
print(ans)

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    n = int(input())
    A_list = list(map(int, input().split()))
    B_list = list(map(int, input().split()))

    result = 0
    for i in range(n):
        current_b = B_list[i]
        rest_b = current_b - A_list[i]
        if rest_b > 0:
            result += A_list[i]
            rest_next_a = A_list[i+1] - rest_b
            if rest_next_a > 0:
                result += rest_b
                A_list[i+1] = rest_next_a
            else:
                result += A_list[i+1]
                A_list[i+1] = 0

        else:
            result += current_b

    print(result)


if __name__ == '__main__':
    main()

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 A[n] <= B[n]:
        cnt += A[n]
        B[n] -= A[n]
        if A[n+1] < B[n]:
            cnt += A[n+1]
            A[n+1] = 0
        else:
            cnt += B[n]
            A[n+1] -= B[n]
    else:
        cnt += B[n]
        
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):
    a = A[i]
    b = B[i]
    c = A[i + 1]
    if b <= a:
        ans += b
    else:
        b -= a
        ans += a
        if b < c:
            ans += b
            c -= b
            A[i + 1] = c
        else:
            A[i + 1] = 0
            ans += c

print(ans)

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

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()))
count=0
for i,b in enumerate(B):
    attack=min(b,A[i]+A[i+1])
    count+=attack
    A[i+1]-=max(0,attack-A[i])
print(count)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
score = 0
for _ in range(n):
    a = A.pop()
    aa = A.pop()
    b = B.pop()
    if (a+aa-b) >=0:
        A.append(min(aa,a+aa-b))
        score += b
    else:
        A.append(0)
        score += a+aa
print(score)

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

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

C = [0] * N

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

print(sum(C))

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

for i in range(n):
    hunted1 = min(a[i],b[i])
    a[i] -=hunted1
    b[i] -=hunted1
    hunted2 = min(a[i+1],b[i])
    a[i+1] -=hunted2
    b[i] -=hunted2
    
print(mosters-sum(a))

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

ans = 0
for i, v in enumerate(b):
    for j in range(2):
        m =  min(a[i+j], v)
        a [i+j] -= m
        v -= m
        ans += m
print(ans)

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

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

print(sumA - sum(A))

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

Solution (Python):
import numpy as np

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

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

print(ans)

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

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

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

Solution (Python):

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



print(ans+tmp)

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

res = 0
for i in range(N):
  a = A[i]
  b = B[i]
  d = max(min(a, b), 0)
  res += d
  
  r = b - d
  if r > 0:
    d = min(A[i+1], r)
    A[i+1] -= d
    res += d
print(res)

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

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

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

monster = 0

for i in range(N):

    if A[i] >= B[i]:
        monster += B[i]
        A[i] -= B[i]
        B[i] = 0

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

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

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




print(monster)

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    A=LI()
    B=LI()
    
    ans=0
    
    for i in range(N):
        if A[i]>=B[i]:
            ans+=B[i]
        else:
            ans+=A[i]
            rem=B[i] - A[i]
            if A[i+1]>=rem:
                ans+=rem
                A[i+1]-=rem
            else:
                ans+=A[i+1]
                A[i+1]=0
                
                
    print(ans)
            
        

main()

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

allMonsters = sum(A)

defeatMonsters = 0
for i in range(N):

    if A[i] <= B[i]:
        defeatMonsters += A[i]
        B[i] -= A[i]

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

    else:
        defeatMonsters += B[i]
        B[i] = 0

print(defeatMonsters)

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

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

total = sum(a)

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

print(total - sum(a))

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

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

Solution (Python):




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

f1 = min(a[0], b[0])
f2 = min(a[1], b[0] - f1)
res = f1 + f2
for i in range(1, n):
    f1 = min(a[i] - f2, b[i])
    f2 = min(a[i+1], b[i] - f1)
    res += f1 + f2

print(res)

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

Solution (Python):
N = int(input())
monsters = [int(m) for m in input().split(" ")]
heroes = [int(h) for h in input().split(" ")]

cnt = 0

for i in range(N):
  if monsters[i] <= heroes[i]:
    cnt += monsters[i]
    heroes[i] -= monsters[i]
    monsters[i] = 0
  else:
    cnt += heroes[i]
    monsters[i] -= heroes[i]
    heroes[i] = 0
    
  if heroes[i] != 0:
    if monsters[i + 1] <= heroes[i]:
      cnt += monsters[i + 1]
      heroes[i] -= monsters[i + 1]
      monsters[i + 1] = 0
    else:
      cnt += heroes[i]
      monsters[i + 1] -= heroes[i]
      heroes[i] = 0
      
print(cnt)

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

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

print(monsters)

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

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

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

    
print(ans)

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

ans,ov = 0,0

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

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

Solution (Python):
n = int(input())
monsters = list(map(int, input().split()))
braves = list(map(int, input().split()))
destroys = 0
for i in range(n):
    if braves[i] > monsters[i]:
        destroys += monsters[i]
        braves[i] -= monsters[i]

        if braves[i] > monsters[i + 1]:
            destroys += monsters[i + 1]
            monsters[i + 1] = 0
        
        else:
            destroys += braves[i]
            monsters[i + 1] -= braves[i]
    else:
        destroys += braves[i]
print(destroys)

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

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

cnt=0

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