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









INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n=INT()
    A=LIST()
    B=LIST()
    ans=0
    ta=A[0]
    for i in range(n):
        tb=B[i]
        ans+=min(ta,tb)
        ta,tb=max(0,ta-tb),max(0,tb-ta)
        ta+=A[i+1]
        ans+=min(ta,tb)
        ta=max(0,ta-tb)
        ta=min(ta,A[i+1])
        
    print(ans)

if __name__=='__main__':
    do()

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

Solution (Python):

import sys
input=sys.stdin.readline
import numpy as np

def main():
    n = int(input())
    mob=np.array(list(map(int,input().split())),dtype=int)
    bra=np.array(list(map(int,input().split())),dtype=int)
    ans=0

    for i in range(n):
        tmp1=min(mob[i],bra[i])
        ans += tmp1
        tmp2=min(mob[i+1],bra[i]-tmp1)
        ans += tmp2
        mob[i+1]-=tmp2
    print(ans)


if __name__=="__main__":
    main()

Solution (Python):

import sys
input=sys.stdin.readline
import numpy as np

def main():
    n = int(input())
    mob=np.array(list(map(int,input().split())),dtype=int)
    bra=np.array(list(map(int,input().split())),dtype=int)
    ans=0

    for i in range(n):
        ans += min(mob[i],bra[i])
        ans += min(mob[i+1],bra[i]-min(mob[i],bra[i]))
        mob[i+1]-=min(mob[i+1],bra[i]-min(mob[i],bra[i]))
    print(ans)


if __name__=="__main__":
    main()

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

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

assert len(A) == N + 1
assert len(B) == N

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

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

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

for i in range(1,len(list_a)):
    temp = list_a[i] - min((list_a[i-1] - list_b[i-1]),0)*(-1)
    if list_a[i-1] - list_b[i-1] <= 0:
        list_a[i-1] = 0
    else:
        list_a[i-1] -= list_b[i-1]
    if temp != list_a[i] and temp >= 0:
        list_a[i] = temp
    elif temp <= 0:
        list_a[i] = 0

del_c = sum(list_a)

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

print(ans)

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):
        if A[i] >= B[i]:
            ans += B[i]
        else:
            ans += A[i]
            remain = B[i] - A[i]
            if A[i+1] >= remain:
                ans += remain
                A[i+1] -= remain
            else:
                ans += A[i+1]
                A[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):
  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(i) for i in input().split()]
B = [int(i) for i in input().split()]
ans = 0
for i in range(N):
    p = A[i] + A[i+1]
    if(p - B[i] >= 0):
        ans += B[i]
        if(A[i] - B[i] < 0):
            A[i+1] = p - B[i]
    else:
        ans += p
        A[i+1] = 0
print(ans)

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

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

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

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

    print(ans)

if __name__ == "__main__":
    main()

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

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

print(res)

Solution (Python):

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

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

print(ans)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
num = 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]:
      a[i+1]=0
    elif a[i+1]>b[i]:
      a[i+1] = a[i+1]-b[i]
  elif a[i]>b[i]:
    a[i] = a[i]-b[i]

num = num-sum(a)
print(num)

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

Solution (Python):
def resolve():
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    f = [0] * (n+1)
    f[0] += min(a[0],b[0])
    f[1] += min(a[1],b[0]-f[0])
    a[1] -= f[1]
    for i in range(1,n):
        f[i] += min(a[i],b[i])
        f[i+1] += min(a[i+1],b[i]-min(a[i],b[i]))
        a[i+1] -= f[i+1]
    print(sum(f))
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]
        a[i]-=b[i]
        b[i]=0
    else:
        ans+=a[i]
        b[i]-=a[i]
        a[i]=0
        if a[i+1]>=b[i]:
            ans+=b[i]
            a[i+1]-=b[i]
            b[i]=0
        else:
            ans+=a[i+1]
            b[i]-=a[i+1]
            a[i+1]=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(0,N):
    
    
    if B[i] <= A[i]:
        cnt += B[i]
    elif B[i] > A[i] and B[i] <= A[i]+A[i+1]:
        cnt += B[i]
        A[i+1] = 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()))
ans = 0
B.append(0)

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

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

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

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

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

        m = min(A[i], B[i])
        ans += m
        A[i] -= m
        B[i] -= m

    print(ans)

if __name__ == "__main__":
    main()

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

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

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

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

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

Solution (Python):
n = int(input())
alst = list(map(int, input().split()))
blst = list(map(int, input().split()))
ans = 0
car = 0
for i in range(n):
  plus = min(blst[i] + car, alst[i])
  ans += plus
  if plus == alst[i]:
    car = blst[i] + car - alst[i]
    car = min(car, blst[i])
  else:
    car = 0
ans += min(car, alst[-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(len(B)):
    cB = B[i]
    if A[i] > cB:
        ans += cB
    else:
        ans += A[i]
        cB -= A[i]
        if cB > A[i+1]:
            ans += A[i+1]
        else:
            ans += cB
        A[i+1] = max(0, A[i+1]-cB)
print (ans)

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

ans = 0
for i in range(n):
    t1 = min(aa[i], bb[i])
    aa[i] -= t1
    bb[i] -= t1
    ans += t1
    t2 = min(aa[i+1], bb[i])
    aa[i+1] -= t2
    ans += t2
print(ans)

Solution (Python):

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

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



print(count)

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

ans = 0
for i in range(n):
    t1 = min(aa[i], bb[i])
    aa[i] -= t1
    bb[i] -= t1
    ans += t1
    t2 = min(aa[i+1], bb[i])
    aa[i+1] -= t2
    bb[i] -= t2
    ans += t2
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]
        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):
def abc135c():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        v = min(b[i], a[i])
        ans += v
        b[i] -= v
        vv = min(a[i+1], b[i])
        a[i+1] -= vv
        ans += vv
    print(ans)
abc135c()

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
  A[i] -= left
  B[i] -= left

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

Solution (Python):



from math import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
import heapq














import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7


INF = float('inf')
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
    return int(readline())
n = I()
A = readInts()
B = readInts()
ans = 0
for i in range(n):
    v = B[i]
    
    nya = min(v, A[i])
    A[i] -= nya
    v -= nya
    ans += nya
    nya = min(A[i+1], v)
    ans += nya
    A[i+1] -= nya
print(ans)

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

Solution (Python):
from collections import deque,defaultdict,Counter
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from bisect import bisect_left,bisect_right
from math import sqrt,gcd,ceil,floor,factorial

from functools import reduce
from pprint import pprint
from statistics import mean,median,mode
import sys
sys.setrecursionlimit(10 ** 6)

INF = float('inf')

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse=False):
    data.sort(key=lambda x:x[col],reverse=revese)
    return data

def mymax(data):
    M = -1*float('inf')
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float('inf')
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def mydictvaluesort(dictionary):
    return sorted( dictionary.items(), key=lambda x:x[1] )

def mydictkeysort(dictionary):
    return sorted( dictionary.items(), key=lambda x:x[0] )

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(' ')
        elif type(ls[0])==str:
            print(' '.join(ls))
        elif type(ls[0])==int:
            print(' '.join(map(str,ls)))
        else:
            print('Output Error')
    else:
        if len(ls)==0:
            print('')
        elif type(ls[0])==str:
            print(''.join(ls))
        elif type(ls[0])==int:
            print(''.join(map(str,ls)))
        else:
            print('Output Error')

def I():
    return int(input())

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

def RI():
    return list(map(int,input().split()))

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]







mod = 10**9 + 7

n = I()
a = RI()
b = RI()
d = list(reversed(a))
e = list(reversed(b))

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

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

count = 0

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

print(count)

Solution (Python):
n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split())) + [0]
ans, tmp_b = 0, 0

for i in range(n+1):
    ans += min(al[i], tmp_b+bl[i])
    tmp_b = max(0, bl[i] - max(0, al[i]-tmp_b))
    

print(ans)

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

Solution (Python):
import numba
from numba import njit, i8
import sys
import numpy as np
read = sys.stdin.readline


@njit((i8, i8[:], i8[:]), cache=True)
def main(n, a, b):
    ans = 0
    for i in range(n):
        d = min(a[i], b[i])
        ans += d
        b[i] -= d
        e = min(a[i + 1], b[i])
        ans += e
        a[i + 1] -= e
    print(ans)


if __name__ == '__main__':
    n = int(input())
    a = np.fromstring(read(), dtype=np.int64, sep=' ')
    b = np.fromstring(read(), dtype=np.int64, sep=' ')
    main(n, a, b)

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

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

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

S_=sum(A)

print(S-S_)

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    a = LI()
    b = LI()
    ans = 0

    for i in range(n):
        tem = a[i]
        a[i] = max(0,a[i]-b[i])
        attack = tem-a[i]
        b[i] -= attack
        ans += attack

        tem = a[i+1]
        a[i+1] = max(0,a[i+1]-b[i])
        attack = tem-a[i+1]
        ans += attack
    
    print(ans)


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

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

ans = 0
carry = 0
for a, b in zip(A, B):
    a2 = max(0, a - carry)

    if a2 < b:
        ans += a
        carry = b - a2
    else:
        ans += carry + b
        carry = 0

ans += min(carry, A[-1])
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):
    b = B[i]
    if B[i] <= A[i]:
        cnt += B[i]
    elif B[i] <= A[i] + A[i + 1]:
        cnt += B[i]
        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()))
x = sum(a)
b = list(map(int,input().split()))
for i in range(n):
    if (a[i] < b[i]):
        tmp = b[i] - a[i]
        a[i + 1] = a[i + 1] - tmp
        a[i] = 0
        if (a[i + 1] < 0):
            a[i + 1] = 0
    else:
        a[i] = a[i] - b[i]

print(x - sum(a))

Solution (Python):
def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    B = [int(i) for i in input().split()]
    ans = 0
    r = A[0]
    for i in range(1, N + 1):
        a = A[i]
        b = B[i - 1]
        ans += min(r + a, b)
        r = max(min(r + a - b, a), 0)
    print(ans)

if __name__ == "__main__":
    solve()

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

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


if __name__ == '__main__':
    main()

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


ans -= sum(A)
print(ans)

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

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

Solution (Python):
import copy

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

ans = 0

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

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

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

ans = 0
c = 0
for i in range(1, n+1):
    a = a_l[-i]
    if b_l[-i] + c >= a:
        ans += a
        d = c-a
        if d >=0:
            d=0
        c = b_l[-i] - abs(d)
    else:
        ans += b_l[-i] + c
        c = 0

if a_l[0] >= c:
    ans+= c
else:
    ans+= a_l[0]
print(ans)

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

cnt = 0
for i in range(n-1, -1,-1):
    if mons[i+1] < kill[i]:
        cnt += mons[i+1]
        kill[i] -= mons[i+1]
        mons[i+1] = 0
    else:
        cnt += kill[i]        
        mons[i+1] -= kill[i]
        kill[i] = 0
    if mons[i] < kill[i]:
        cnt += mons[i]
        kill[i] -= mons[i]
        mons[i] = 0
    else:
        cnt += kill[i]
        mons[i] -= kill[i]
        kill[i] = 0
        
print(cnt)

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

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

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

print(ans)

Solution (Python):

"""
Created on Thu Sep 10 11:20:10 2020

@author: liang
"""

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

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

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

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