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):
from heapq import heappush, heappop
import re
from collections import deque
import sys
import math
input = sys.stdin.readline


def int_raw():
    return int(input())


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


def ints_raw():
    return tuple(map(int, ss_raw()))


DIV = 10**9+7


def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret


def main():
    N = int_raw()
    As = ints_raw()
    Bs = ints_raw()
    ans = 0
    resB = 0
    for i in range(N):
        Bi = Bs[i]
        Ai = As[i] 
        ans += min(Bi+resB,Ai)

        resB = max(Bi-max(Ai-resB,0),0)

    return ans+min(resB,As[-1])
print(main())

Solution (Python):
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations

from itertools import accumulate
from bisect import bisect_left, bisect_right

import re



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


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e18)
MOD = int(1e9)+7 


def create_grid(W, H, value = 0):
    return [[ value for _ in range(H)] for _ in range(W)]



N = i_inpl()
A = l_inpl()
B = l_inpl()

ans = 0

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

print(ans)

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

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

print(x)

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

pre = 0
ans = 0
for i, a in enumerate(A):
    if i == N:
        ans += min(pre, a)
        break
    if a <= pre:
        ans += a
        pre = B[i]
    elif pre < a < pre + B[i]:
        ans += a
        pre = B[i]+pre - a
    else:
        ans += pre + B[i]
        pre = 0

print(ans)

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

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

    kill += x1 + x2

print(kill)

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

print(count)

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

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

        ans += min(A[i+1], B[i])
        A[i+1] = max(0, A[i+1] - B[i])
    else:
        ans += B[i]
        A[i] -= B[i]
print(ans)

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

cnt=0

for i in range(n):
    for j in range(2):
        x=a[i+j]
        y=b[i]
        m=min(x,y)
        b[i]-=m
        a[i+j]-=m
        cnt+=m

print(cnt)

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

cnt = 0
beaten = min(a[0], b[0])
b[0] -= beaten
cnt += beaten
for i in range(1, len(b)):
    beaten = min(a[i], b[i-1])
    a[i] -= beaten
    cnt += beaten
    beaten = min(a[i], b[i])
    b[i] -= beaten
    cnt += beaten
beaten = min(a[len(a)-1], b[len(b)-1])
cnt += beaten

print(cnt)

Solution (Python):
n=int(input())
T=0;
p=input().rstrip().split(' ')
q=input().rstrip().split(' ')
for i in range(0,len(q)):
    A=int(q[i])
    B=int(p[i])
    C=int(p[i+1])
    if A>=B:
        T+=B;
        A=A-B;
        if(A>=C):
            T+=C;
            p[i+1]=0;
        else:
            p[i+1]=int(p[i+1])-A;
            T+=A;
    else:
        T+=A;
print(T)

Solution (Python):
n = int(raw_input())
a = map(int,raw_input().split())
b = map(int,raw_input().split())
ans = 0
for i in range(n):
  temp = min(a[i],b[i])
  a[i] -= temp
  b[i] -= temp
  ans += temp
  temp = min(a[i+1],b[i])
  a[i+1] -= temp
  b[i]   -= temp
  ans += temp
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):
  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):
from sys import stdin

N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]
cnt = 0

for i in range(N):
    kill = min(A[i], B[i])
    A[i] -= kill
    cnt += kill
    B[i] -= kill
    kill2 = min(A[i + 1], B[i])
    A[i + 1] -= kill2
    cnt += kill2

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]
        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())
mosters = list(map(int, input().split()))
heros = list(map(int, input().split()))
cnt = 0

for i in range(N):
    if heros[i] <= mosters[i]:
        cnt += heros[i]
    else:
        cnt += mosters[i]
        if (heros[i] - mosters[i]) >= mosters[i+1]:
            cnt += mosters[i+1]
            mosters[i+1] = 0
        else:
            cnt += heros[i] - mosters[i]
            mosters[i+1] -= (heros[i] - mosters[i])

print(cnt)

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

cnt = 0

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

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

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

print(cnt)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
num = 0
for i in range(N):
    left = min(A[i], B[i])
    num += left
    B[i] -= left
    right = min(A[i+1], B[i])
    num += right
    A[i+1] -= right
print(num)

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

ans = 0
for i in range(N):
    added = min(As[i], Bs[i])
    Bs[i] -= added
    added2 = min(As[i + 1], Bs[i])
    As[i + 1] -= added2
    ans += added + added2
print(ans)

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

dead = 0
yuusya_zan = 0
for i, yuusya in enumerate(b):
    monster = a[i]

    if yuusya_zan >= monster:
        dead += monster
        monster = 0
    else:
        dead += yuusya_zan
        monster -= yuusya_zan

    if yuusya >= monster:
        yuusya -= monster
        dead += monster
        yuusya_zan = yuusya
    else:
        dead += yuusya
        yuusya_zan = 0

monster = a[n]
if yuusya_zan >= monster:
    dead += monster
    monster = 0
else:
    dead += yuusya_zan
    monster -= yuusya_zan

print(dead)

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

    else:
        ans += b
print(ans)

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

taos=0
for x in range(n):
    if blist[x]>=alist[x]:
        taos+=alist[x]
        blist[x]=blist[x]-alist[x]
        alist[x]=0
        if blist[x]>=alist[x+1]:
            taos+=alist[x+1]
            alist[x+1]=0
        else:
            taos+=blist[x]
            alist[x+1]-=blist[x]
    else:
        taos+=blist[x]
        alist[x]=alist[x]-blist[x]

print(taos)

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

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

    left_b = b[i] - _counter
    if left_b > 0:
        __counter = min(left_b, a[i + 1])
        a[i + 1] -= __counter
        counter += __counter

print(counter)

Solution (Python):
ll= lambda : list(map(int,input().split()))
testcases=1

for testcase in range(testcases):
    [n]=ll()
    a=[0]+ll()
    b=[0]+ll()
    ans=0
    
    for i in range(1,n+1):
        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]):
            ans+=b[i]
            b[i]-=a[i]
            a[i]=0
            
            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()))
c = [0] * (n + 1)
for h in range(n):
    k = min(b[h], a[h])
    c[h] = c[h] + k
    a[h] = a[h] - k
    
    k = min(b[h] - k, a[h + 1])
    c[h + 1] = c[h + 1] + k
    a[h + 1] = a[h + 1] - k
    
print(sum(c))

Solution (Python):
import copy
import sys
sys.setrecursionlimit(4100000)

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

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

Solution (Python):

N = int(input())
As =   list(map(int,input().split(" ")))
Bs =   list(map(int,input().split(" ")))
ans = 0
for i in range(N):
    ans += min(As[i],Bs[i]) 
    Bs[i] = max(Bs[i] - As[i] , 0 ) 
    ans +=min(As[i + 1],Bs[i]) 
    As[i + 1] = max(As[i + 1] - Bs[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, b in enumerate(B):

    if b >= A[i]:
        cnt += A[i]
        b -= A[i]

        if b >= A[i+1]:
            cnt += A[i+1]
            A[i+1] = 0
        else:
            cnt += b
            A[i+1] -= b
    else:
        cnt += b
print(cnt)

Solution (Python):




def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    killed = 0
    remain = A[0]
    for i, b in enumerate(B):
        target = remain + A[i + 1]
        power = B[i]
        killed += min(target, power)
        remain = max(min(target - power, A[i + 1]), 0)
    print(killed)


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

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

    print(cnt)
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    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]:
            a[i] -= b[i]
            count += b[i]
        else:
            count += a[i]
            b[i] -= a[i]
            a[i] = 0
            if a[i + 1] >= b[i]:
                a[i + 1] -= b[i]
                count += b[i]
            else:
                count += a[i + 1]
                b[i] -= a[i + 1]
                a[i + 1] = 0
    print(count)


if __name__ == '__main__':
    main()

Solution (Python):


def readInts():
  return [int(s) for s in input().split(" ")]

def main():
  N = readInts()[0]
  A = readInts()
  B = readInts()

  ans = 0
  carry = 0
  B.append(0)
  for i, b in enumerate(B):
    n = min(A[i], carry)
    restA = A[i] - n
    m = min(restA, b)
    ans += n + m
    carry = b - m
  print(ans)

if __name__ == "__main__":
  main()

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

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

ans=0
rem=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)

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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l1=LI()
  l2=LI()

  ans=0
  for i in range(n):
    x=l2[i]
    y=l1[i]
    if y<=x:
      ans+=y
      x-=y

      if l1[i+1]<=x:
        ans+=l1[i+1]
        l1[i+1]=0
      else:
        ans+=x
        l1[i+1]-=x

    else:
      ans+=x

  return ans


print(main())

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

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

print(ans)

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

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

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

ans_max = sum(A)

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

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

ans_min = sum(A)

ans = ans_max - ans_min

print(ans)

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

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

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

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

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

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

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

print(ans)

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

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

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):
    if a[i] > b[i]:
        ans += b[i]
    elif 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]

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

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

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


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

print(m)

Solution (Python):

import sys

def debug(*args):
    print(*args, file=sys.stderr)


def solve(N, A, B):
    B = B

    result = 0
    for i in range(0, N):
        r0 = min(A[i], B[i])
        A[i] -= r0
        B[i] -= r0
        result += r0
        debug("i=%d"%i, r0)
        if B[i] > 0:
            r = min(A[i+1], B[i])
            A[i+1] -= r
            B[i] -= r
            result += r
            debug("i=%d"%i, r)

    return result


def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]
    print(solve(N, A, B))

main()

Solution (Python):
''' بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ '''
gi = lambda : list(map(int,input().strip().split()))
n, = gi()
a = gi()
b = gi()
ans = 0
for k in range(n):
	if b[k] >= a[k]:
		ans += a[k]
		b[k] -= a[k]
	else:
		ans += b[k]
		a[k] -= b[k]
		b[k] = 0
	if b[k] > 0:
		temp = min(a[k + 1], b[k])
		a[k + 1] -= temp
		ans += temp
print(ans)

Solution (Python):
import numpy as np

n = int(input())
arr_base = list(map(int,input().split()))
cap_base = list(map(int,input().split()))

arr = arr_base.copy()
cap = cap_base.copy()

ans = 0
for i in range(n):
    if arr[i] < cap[i]:
        ans += arr[i]
        sup = cap[i]-arr[i]
        if sup > arr[i+1]:
            ans += arr[i+1]
            arr[i+1] = 0

        else:
            arr[i+1]-=sup
            ans += sup
    else:
        ans += cap[i]
temp = ans
ans = 0


cap = cap_base.copy()
arr= arr_base.copy()
for i in range(1,n+1):
    if arr[-i] < cap[-i]:
        ans += arr[-i]
        sup = cap[-i]-arr[-i]
        if sup > arr[-i-1]:
            ans += arr[-i-1]
            arr[-i-1] = 0
        else:
            arr[-i-1]-=sup
            ans += sup
    else:
        ans += cap[-i]




print(max(temp,ans))

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

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

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

print(ans+min(A[-1],p))

Solution (Python):
N = int(input())
li = list(map(int,input().split()))	
li2 = list(map(int,input().split()))	

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

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

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


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

print(ans)

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

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

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

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

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

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

print(s)

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

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

print(ans)

Solution (Python):











import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    B=LI()
    A=LI()
    ans = 0
    for i in range(N):
        ans += max(0,min(A[i],B[i]))
        A[i] -= max(0,B[i])
        ans += max(0,min(A[i],B[i+1]))
        B[i+1]-=max(0,A[i])
    
    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-1, -1, -1):
    if A[i+1] >= 1:
        kill = min(B[i], A[i+1])
        B[i] -= kill
        A[i+1] -= kill
        ans += kill
    kill = min(B[i], A[i])
    B[i] -= kill
    A[i] -= kill
    ans += kill
print(str(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]:
        ans += a[i]
        b[i] -= a[i]
    else:
        ans += b[i]
        b[i] = 0

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

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

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

Solution (Python):
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy




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

    ans = 0

    k = min(A[0], B[0])
    ans += k
    A[0] -= k
    B[0] -= k

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

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

    k = min(A[-1], B[-1])
    ans += k

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

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

print(bustered_counter)

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

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

print(total - sum(a))

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

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

Solution (Python):
n = int(input())
a_s = [int(item) for item in input().split()]
b_s = [int(item) for item in input().split()]
total = sum(a_s)

for i, b in enumerate(b_s):
    if b <= a_s[i]:
        a_s[i] -= b
        continue
    elif b > a_s[i]:
        a_s[i+1] = max(0, a_s[i+1] - (b - a_s[i]))
        a_s[i] = 0
    
print(total - sum(a_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):
    left = min(A[i], B[i])
    A[i] -= left
    B[i] -= left
    ans += left

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

print(ans)