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())
monster = list(map(int, input().split()))
hero = list(map(int, input().split()))

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

Solution (Python):

n, p, *a = map(int, open(0).read().split())
c = 0
for a, b in zip(a, a[n:]):
    c += min(p+a, b)
    p = a - max(min(b-p, a), 0)
print(c)

Solution (Python):
n, p, *a = map(int, open(0).read().split())
c = 0
for a, b in zip(a, a[n:]):
    c += min(p+a, b)
    p = a - max(min(b-p, a), 0)
print(c)

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

Solution (Python):
def main():
    N = int(input())
    *a, = map(int, input().split())
    *b, = map(int, input().split())
    b += [0]

    total_beaten = 0
    carry = 0
    for beat, number in zip(b, a):
        by_prev = min(number, carry)

        total_beaten += by_prev
        number -= by_prev

        by_curr = min(number, beat)
        total_beaten += by_curr

        carry = beat - by_curr

    print(total_beaten)


if __name__ == '__main__':
    main()

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

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

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

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

print(ans-sum(B))

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

        a[i+1] = max(a[i+1] - (b[i] - a[i]),0)
        a[i] = 0

        

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

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 reversed(range(N)):
        reduce2 = min(A[i+1],B[i])
        reduce1 = min(A[i], B[i]-reduce2)
        A[i] -= reduce1
        ans += reduce1 + reduce2
    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
for i in range(n):
    min_ = min(b[i], a[i])
    ans += min_
    b[i] -= min_
    a[i] -= min_
    
    min_ = min(b[i], a[i + 1])
    ans += min_
    b[i] -= min_
    a[i + 1] -= min_
print(ans)

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

monsall = 0
monsnext = a[0]

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

print(monsall)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))[::-1]
B = list(map(int, input().split()))[::-1]
ans = 0

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

print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans=0
for i,bi in enumerate(b):
    x = min(a[i],bi)
    bi-=x
    a[i]-=x
    ans+=x
    if bi>0:
        x=min(a[i+1],bi)
        a[i+1]-=x
        ans+=x
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+=A[i]
        tmp = min(B[i]-A[i], A[i+1])

        A[i+1]-=tmp
        ans+=tmp
    else:
        ans+=B[i]

print(ans)

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

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

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
SUM=sum(A)
for i in range(N):
    res=B[i]
    fir=min(res,A[i])
    A[i]=A[i]-fir
    res-=fir
    sec=min(res,A[i+1])
    A[i+1]-=sec

print(SUM-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 a[i] <=  b[i]:
        ans += a[i]
        b[i] -= a[i]
    else:
        ans += b[i]
        b[i] = 0
    if b[i] > 0:
        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):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  



N = read_a_int()
A = read_ints()
B = read_ints()
ans = 0
for i in ra(N):
    power = B[i]
    left = max(A[i] - power, 0)  
    used = A[i] - left
    ans += used  
    power -= used

    
    left = max(A[i + 1] - power, 0)
    used = A[i + 1] - left
    A[i + 1] = left
    ans += used  
print(ans)

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

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

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

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


print(kill_score)

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

def solve(N,A,B):
    ans = 0
    for i in range(N):
        battle = min(A[i],B[i])
        B[i] -= battle
        ans += battle
        nex = min(A[i+1],B[i])
        A[i+1] -= nex
        ans += nex
    return ans
print(solve(N,A,B))

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

        a[i]=0

print(s-sum(a))

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    N=int(readline())
    A=list(map(int,readline().split()))
    B=list(map(int,readline().split()))

    res=0
    for i in range(N):
        if A[i]>=B[i]:
            res+=B[i]
        elif A[i]<B[i]:
            res+=A[i]
            p=B[i]-A[i]
            res+=min(p,A[i+1])
            A[i+1]=max(A[i+1]-p,0)
    print(res)
if __name__=="__main__":
    main()

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

Solution (Python):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
B = lr()
remain = 0
answer = 0
for i in range(N):
    attack = min(A[i], remain)
    answer += attack
    A[i] -= attack
    if A[i] > 0:
        attack = min(A[i], B[i])
        answer += attack
        B[i] -= attack
    remain = B[i]

answer += min(A[N], remain)
print(answer)

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

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


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

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]
    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)
  return

if __name__ == "__main__":
  resolve()

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

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

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

print (ans)

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
res = 0
for i in range(N):
  if b[i] >= a[i]:
    b[i] -= a[i]
    res += a[i]
  else:
    res += b[i]
    b[i] = 0
  res += min(a[i + 1], b[i])
  a[i + 1] = max(0, a[i + 1] - b[i])
print(res)

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


if __name__ == '__main__':
    main()

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

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

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

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

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

Solution (Python):
ma = lambda : map(int,input().split())
n = int(input())
A = list(ma())
B = list(ma())
ans = 0
tmp = 0
for i in range(n):
    for j in range(2):
        tmp = B[i]
        ans += min(B[i],A[i+j])
        B[i] =max(B[i]-A[i+j],0)
        A[i+j] = max(0,A[i+j]-tmp)
print(ans)

Solution (Python):
n = int(input())
*a, = map(int, input().split())
*b, = map(int, input().split())
s = sum(a)
for i in range(n):
    c = min(a[i], b[i])
    a[i] -= c
    b[i] -= c
    a[i+1] = max(0, 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()))
monsters = sum(A)

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

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

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

print(ans)

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, b in enumerate(b):
    if b > a[i]:
        b -= a[i]
        num += a[i]
        if b > a[i+1]:
            num += a[i+1]
            a[i+1] = 0
        else:
            a[i+1] -= b
            num += b
    else:
        num += b

print(num)

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

for i in range(N):
    if A[i] >= B[i]:
        cnt += B[i]
    else:
        tmp = B[i] - A[i]
        cnt += A[i]
        if A[i+1] >= tmp:
            cnt += tmp
            A[i+1] = A[i+1] - tmp
        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()))

ans = 0

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

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
ans = 0
for i in range(N):
    left = min(A[i], B[i])
    B[i] -= left
    ans += left
    right = min(A[i + 1], B[i])
    A[i + 1] -= right
    B[i] -= right
    ans += right
 
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] -= abs(A[i] - B[i])
        A[i + 1] *= int(A[i + 1] > 0)
        A[i] = 0 

print(ans - sum(A))

Solution (Python):
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd 
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) 







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

print(sum_a - sum(a))

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

    ori = sum(a)

    for i in range(n):
        bt = b[i]
        if a[i] < bt:

            a[i+1] = max(a[i+1]-(bt-a[i]), 0)
            a[i] = 0
        else:
            a[i] -= bt
    end = sum(a)
    print(ori-end)
resolve()

Solution (Python):
def main():
	n = int(input())
	arr = list(map(int,input().split()))
	arr2 = list(map(int,input().split()))
	cnt = 0
	if arr2[0]>arr[0]:
		cnt+=arr[0]
		arr2[0]-=arr[0]
	else:
		cnt+=arr2[0]
		arr2[0]=0
	for i in range(1,len(arr)-1):
		if arr[i]<(arr2[i]+arr2[i-1]):
			cnt+=arr[i]
			arr[i]-=arr2[i-1]
			if arr[i]>0:
				arr2[i]-=arr[i]
		else:
			cnt+=(arr2[i]+arr2[i-1])
			arr2[i]=0
	if arr2[-1]>arr[-1]:
		cnt+=arr[-1]
		arr2[-1]-=arr[-1]
	else:
		cnt+=arr2[-1]
		arr2[-1]=0
	print(cnt)

main()

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

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

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

        elif A[i] <= B[i] <= A[i] + A[i+1]:
            cnt += B[i]
            A[i+1] -= B[i] - A[i]
            A[i] = 0

        elif A[i] + A[i+1] < B[i]:
            cnt += A[i] + A[i+1]
            A[i+1] = 0
            A[i] = 0

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

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

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

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

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

s = str(sum(a))

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

print(int(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 a[i] < b[i]:
    b[i] -= a[i]
    ans += 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_nums = list(map(int, input().split()))
b_nums = list(map(int, input().split()))


ans_monsters = 0


for i in range(len(b_nums)):
    
    if b_nums[i]>=a_nums[i]:
        ans_monsters += a_nums[i]
        b_nums[i] -= a_nums[i]
    else:
        ans_monsters += b_nums[i]
        b_nums[i] = 0

    
    if b_nums[i]>0:
        if b_nums[i]>=a_nums[i+1]:
            ans_monsters += a_nums[i+1]
            a_nums[i+1] = 0
        else:
            ans_monsters += b_nums[i]
            a_nums[i+1] -= b_nums[i]


print(ans_monsters)

Solution (Python):
n = int(input())
AL = list(map(int, input().split()))
BL = list(map(int, input().split()))
ans = 0
for i in range(n):
    if BL[i] <= AL[i]:
        ans += BL[i]
    elif BL[i] > AL[i] and BL[i] <= AL[i]+AL[i+1]:
        ans += BL[i]
        AL[i+1] = AL[i+1] - BL[i] + AL[i]
    else:
        ans += AL[i]+AL[i+1]
        AL[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 itr, b in enumerate(B):
  rem = b
  for i in range(2):
    dam = min(rem, A[itr + i])
    A[itr + i] -= dam
    ans += dam
    rem -= dam
print(ans)

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

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


if __name__ == '__main__':
    main()

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(N)[::-1]:
        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
            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
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):


import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N=int(input())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))
    cnt=0
    for i in range(N):
        val=min(A[i],B[i])
        A[i]-=val
        B[i]-=val
        cnt+=val

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

    print(cnt)




resolve()

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

A.reverse()
B.reverse()

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

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

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