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]+A[i+1],B[i])
  if A[i]+A[i+1]>=B[i]:
    A[i+1]=min(A[i+1],A[i]+A[i+1]-B[i])
  else:
    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):
    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())
M = list(map(int,input().split()))
Y = list(map(int,input().split()))

c=0
np=0
for i in range(n):
    if M[i]<np :
        c+=M[i]
        np=Y[i]
    elif M[i]<Y[i]+np :
        np = Y[i]+np-M[i]
        c+=M[i] 
    else:
        c+=Y[i]+np
        np = 0 
c+=min(np,M[n])

print(c)

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

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

s = sum(a)

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


print(s-sum(a))

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

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

print(result)

Solution (Python):
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = [0]*(n+1)
    b = list(map(int, input().split()))
    for i in range(n):
        if a[i]-ans[i] >= b[i]:
            ans[i] += b[i]
        else:
            k = ans[i]
            ans[i] += a[i]-k
            rest = b[i] - a[i] + k
            ans[i+1] += min(rest,a[i+1])
    print(sum(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 reversed(range(N)):
  if A[i+1] > B[i]:
    ans += B[i]
  else:
    ans += A[i+1]
    B[i] -= A[i+1]
    if A[i] > B[i]:
      ans += B[i]
      A[i] -= B[i]
    else:
      ans += A[i]
      A[i] = 0
print(ans)

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

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

    for i in range(n):
        x = A[i] + A[i+1]
        ans += min(x,B[i])
        A[i+1] = min(A[i+1],max(x-B[i],0))
    return print(ans)

solv()

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

Solution (Python):
n = int(input().rstrip())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

ans = 0
resid = 0
for i in range(n):
    prev_beat = min(resid, aa[i])
    cur_beat = min(aa[i] - prev_beat, bb[i])
    ans += prev_beat
    ans += cur_beat
    resid = bb[i] - cur_beat
ans += min(resid, aa[n])

print(ans)

Solution (Python):
n = int(input())
a, b = [list(map(int, input().split())) for _ in range(2)]

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

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


solve()

Solution (Python):
def solve():
    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] = min(A[i + 1], max(A[i] + A[i + 1] - B[i], 0))
    print(ans)


solve()

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


solve()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
s = sum(A)
for i, b in enumerate(B):
    if A[i] <= b:
        A[i+1] = max(0, A[i+1] - (b-A[i]))
        A[i] = 0
    else:
        A[i] = A[i] - b
print(s-sum(A))

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

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


n = I()
a = LI()
b = LI()

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

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

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

print(monsters-sum(a))

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

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

cnt=0
for i in range(len(B)):
  a=min(A[i], B[i])
  cnt+=a
  a=min(A[i+1],B[i]-a)
  cnt+=a
  A[i+1]-=a
print(cnt)

Solution (Python):
def resolve():
    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] < B[i] <= A[i] + A[i+1]:
            A[i+1] = A[i+1]-(B[i]-A[i])
            ans += B[i]
        else:
            ans += A[i] + A[i+1]
            A[i+1] = 0
    print(ans)
resolve()

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

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

res = 0

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

    if B[i] > 0 and A[i] > 0:
        if B[i] > A[i]:
            res += A[i]
            A[i] = 0
        else:
            res += B[i]
            A[i] -= B[i]

print(res)

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

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

    else:
        ans += b[i]
        b[i] = 0
        continue

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

    else:
        ans += b[i]
        a[i+1] -= b[i]
        b[i] = 0

print(ans)

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

for n in range(N):
  if B[n]<=A[n]:
    ans+=B[n]
  elif B[n]<=A[n]+A[n+1]:
    ans+=B[n]
    A[n+1]-=B[n]-A[n]
  else:
    ans+=A[n]+A[n+1]
    A[n+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]
            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=list(map(int,input().split()))
b=list(map(int,input().split()))
b.append(0)

ans = sum(a)

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

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

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

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

def dm(i, j):
    d = min(a[i], b[j])
    a[i] -= d; b[j] -= d
    return d

for i in range(n):
    cnt += dm(i, i)
    cnt += dm(i + 1, i)
print(cnt)

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

sumA = sum(A)
a = A[:]
for i, b in enumerate(B):
    if a[i] < b:
        b -= a[i]
        a[i] = 0
        a[i+1] = max(0, a[i+1] - b)
    else:
        a[i] -= b
ans = sumA - sum(a)
a = A[:]
for i in range(N - 1, -1, -1):
    b = B[i]
    if a[i+1] < b:
        b -= a[i+1]
        a[i+1] = 0
        a[i] = max(0, a[i] - b)
    else:
        a[i+1] -= b
ans = max(ans, sumA - sum(a))
print(ans)

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

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

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

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

eriminate_enemy = [0 for _ in range(N+1)]

for index, B in enumerate(Bs):
    if(B <= As[index]):
        eriminate_enemy[index] += B
        continue

    eriminate_enemy[index] += As[index]
    
    if(B <= As[index] + As[index + 1]):
        eriminate_enemy[index + 1] += B - As[index]
        As[index+1] -= eriminate_enemy[index + 1]
    
    else:
        eriminate_enemy[index + 1] += As[index+1] 
        As[index+1] = 0

print(sum(eriminate_enemy))

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque, defaultdict


from collections import Counter

from itertools import accumulate,combinations,permutations,product

from heapq import heapify,heappop,heappush

from functools import reduce,lru_cache

from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): 
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    

    N = int(input())
    
    A = list(map(int, input().split())) 
    B = tuple(map(int, input().split()))
    
    
    cnt=0
    for i in range(N):
        a=A[i];b=B[i]
        if b>a:
            cnt+=min(b,a+A[i+1])
            A[i]=0
            A[i+1]=max(0,A[i+1]-(b-a))
            
        else:
            A[i]=a-b
            cnt+=b
        
    print(cnt)




if __name__ == "__main__":
    main()

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


mons_n = sum(A)


for i in range(N):
    
    power = B[i]
    
    temp = min(A[i], power)
    A[i] -= temp
    power -= temp

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

print(mons_n - sum(A))

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

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

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

ans = 0
for i, b in enumerate(B):
    rem = max(0, b-A[i])
    ans += min(A[i], b)
    ans += min(A[i+1], rem)
    A[i+1] = max(0, A[i+1]-rem)

print(ans)

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

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

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

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

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

Solution (Python):

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


cnt = 0

help_cnt = 0
for i in range(n):
    
    now_kill_cnt = min(a[i]-help_cnt, b[i])

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


print(cnt)

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

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

print(defeat_monster)

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

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

print(monster - sum(a))

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

count=0

D=[A[0]]

for i in range(N):
    s=B[i]-D[i]
    if s>0:
        d=A[i+1]-s
        if d>0:
            d=d
            count+=B[i]
        else :
            d=0
            count+=D[i]+A[i+1]
    else:
        d=A[i+1]
        count+=B[i]
    D.append(d)

print(count)

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

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

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

cnt = 0

for i in range(n):
    t = min(a[i], b[i])
    cnt += t
    a[i] -= t
    b[i] -= t

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

print(cnt)

Solution (Python):























































global FLAG_LOG
FLAG_LOG = False


def log(value):
    
    
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    
    N = int(lines[0])
    
    monsters = list(map(int, lines[1].split()))
    braves = list(map(int, lines[2].split()))
    
    
    
    
    
    

    cnt = 0

    for n in range(N):
        if monsters[n] >= braves[n]:
            cnt += braves[n]
            monsters[n] -= braves[n]
            log(f'cnt=[{cnt}]')
        else:
            
            cnt += monsters[n]
            log(f'cnt=[{cnt}]')
            
            monster = min(braves[n]-monsters[n], monsters[n+1])
            cnt += monster
            log(f'cnt=[{cnt}]')
            monsters[n+1] -= monster

    return [cnt]



def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines



def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['2', '3 5 2', '4 5']
        lines_export = [9]
    if pattern == 2:
        lines_input = ['3', '5 6 3 8', '5 100 8']
        lines_export = [22]
    if pattern == 3:
        lines_input = ['2', '100 1 1', '1 100']
        lines_export = [3]
    return lines_input, lines_export



def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode



def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(3)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_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 i in range(N):
  
  if B[i]>A[i]:
    cnt+=A[i]
    r = B[i]-A[i]
  else:
    cnt+=B[i]
    r = 0
  
  if r>A[i+1]:
    cnt+=A[i+1]
    A[i+1]=0
  else:
    cnt+=r
    A[i+1]-=r

print(cnt)

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

print(ans)

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

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

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 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]
    else:
        ans += 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 in range(N-1,-1,-1):
  n = min(A[i+1],B[i])
  A[i+1] -= n
  B[i] -= n
  count += n
  m = min(A[i],B[i])
  A[i] -= m
  B[i] -= m
  count += m

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):
    d = min(a[i],b[i])
    ans += d
    a[i] -= d
    b[i] -= d
    d = min(a[i+1],b[i])
    ans += d
    a[i+1] -= d
    b[i] -= d

print(ans)

Solution (Python):
N,A,B=open(0).read().rstrip().split('\n')
N=int(N)
A=list(map(int,A.split()))
B=list(map(int,B.split()))
hunt=0
for i in range(N):
    if A[i]>=B[i]:
        hunt+=B[i]
    elif A[i]+A[i+1]>=B[i]:
        hunt+=B[i]
        suplus=B[i]-A[i]
        A[i+1]=A[i+1]-suplus
    else:
        hunt+=A[i]+A[i+1]
        A[i+1]=0
print(hunt)

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

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
cnt = 0
def dm(i, j):
    d = min(a[i], b[j])
    a[i] -= d; b[j] -= d
    return d

for i in range(n):
    cnt += dm(i, i)
    cnt += dm(i + 1, i)
print(cnt)

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):
    dm = min(a[i], b[i])
    b[i] -= dm; cnt += dm
    dmn = min(a[i + 1], b[i])
    a[i + 1] -= dmn; cnt += dmn
print(cnt)

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):
    dm = min(a[i], b[i])
    a[i] -= dm; b[i] -= dm; cnt += dm
    dmn = min(a[i + 1], b[i])
    a[i + 1] -= dmn; b[i] -= dmn; cnt += dmn
print(cnt)

Solution (Python):
N = int(input())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
total = sum(aaa)
for i in range(N - 1, -1, -1):
    if aaa[i + 1] > bbb[i]:
        aaa[i + 1] -= bbb[i]
    else:
        bbb[i] -= aaa[i + 1]
        aaa[i + 1] = 0
        aaa[i] = max(0, aaa[i] - bbb[i])
print(total - sum(aaa))

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

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

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

        if B[i]:
            beaten = min(A[i + 1], B[i])
            A[i + 1] -= beaten
            B[i] -= beaten
            ans += beaten

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())
Bs = list(mapint())

rest = 0
ans = 0
for i in range(N):
    a = As[i]
    b = Bs[i]
    if a>=rest:
        a -= rest
        ans += rest
    else:
        ans += a
        a = 0
    if b>=a:
        rest = b-a
        ans += a
    else:
        rest = 0
        ans += b

ans += min(rest, As[-1])

print(ans)

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

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