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 collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

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

Solution (Python):
n = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
res = 0
for i in range((n + 1)//2):
  if B[i] > A[i]:
    res += min(A[i] + A[i+1], B[i])
    A[i+1] = max(0, A[i] + A[i+1] - B[i])
    A[i] = 0
    B[i] = 0
  else:
    res += B[i]
    A[i] -= B[i]
    B[i] = 0
  if B[n-i-1] > A[n-i]:
    res += min(A[n-i] + A[n-i-1], B[n-i-1])
    A[n-i-1] = max(0, A[n-i-1] + A[n-i] - B[n-i-1])
    A[n-i] = 0
    B[n-i-1] = 0
  else:
    res += B[n-i-1]
    A[n-i] -= B[n-i-1]
    B[n-i-1] = 0
print(res)

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

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

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
A = iil()
B = iil()
rep = 0
ans = 0
for i,item in enumerate(B):
    ans += min(A[i],item+rep)
    rep = max(0, item - max(0, A[i] - rep))
print(ans+min(A[-1],rep))

Solution (Python):
import sys
N=input()
A=map(int, sys.stdin.readline().split())
B=map(int, sys.stdin.readline().split())
before=sum(B)

for i in range(N):
    if B[i]!=0:
        k=min(A[i],B[i])
        A[i]-=k
        B[i]-=k
    if B[i]!=0:
        k=min(A[i+1],B[i])
        A[i+1]-=k
        B[i]-=k
after=sum(B)
print before-after

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

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

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

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

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

    n=int(input())
    A=[int(_) for _ in input().split()]
    sum_a=sum(A)
    B=[int(_) for _ in input().split()]
    for i in range(n):
        s=max(B[i]-A[i],0)
        A[i]-=min(A[i],B[i])
        A[i+1]-=min(A[i+1],s)
    print(sum_a-sum(A))

if __name__=='__main__':
    main()

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

    n=int(input())
    A=[int(_) for _ in input().split()]
    sum_a=sum(A)
    B=[int(_) for _ in input().split()]
    for i in range(n):
        s=max(B[i]-A[i],0)
        A[i]-=min(A[i],B[i])
        A[i+1]-=min(A[i+1],s)
    print(sum_a-sum(A))

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

print(ans)

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


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

Solution (Python):



N = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))


max_monster = 0


for i in range(N):
    b = b_list[i]
    if a_list[i]<=b:
        max_monster += a_list[i]
        b_list[i] -= a_list[i]
        a_list[i] = 0
    else:
        max_monster += b
        b_list[i] = 0

    b = b_list[i]
    if a_list[i+1]<=b:
        max_monster += a_list[i+1]
        a_list[i+1] = 0
    else:
        max_monster += b
        a_list[i+1] -= b_list[i]


print(max_monster)

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

print(ans)

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

ans = 0
for i in range(N):
  if A[i] <= B[i]:
    ans += A[i]
    B[i] -= A[i]
  else:
    ans += B[i]
    B[i] = 0
  if B[i] > 0:
    if A[i+1] > B[i]:
      A[i+1] -= B[i]
      ans += 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()))
count=0
for i in range(N):
	x=A[N-i]+A[N-1-i]
	count+=min(x,B[N-i-1])
	A[N-i-1]=max(0,A[N-i-1]-max(0,B[N-i-1]-A[N-i]))
print(count)

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

ans, mod = 0, 0

for i in range(N):

    if mod > A[i]:
        ans += A[i]
        A[i] = 0
    else:
        ans += mod
        A[i] -= mod
        mod = 0

    if B[i] > A[i]:
        ans += A[i]
        mod = B[i] - A[i]
    else:
        ans += B[i]
        mod = 0

ans += min(mod, A[N])

print(ans)

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

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

A.reverse()
B.reverse()

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


print(cnt)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

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

Solution (Python):
def solve():
    n = int(input())
    b = list(map(int,input().split()))
    a = 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:
            if a[i]>=b[i]+b[i+1]:
                ans+=b[i]+b[i+1]
                b[i+1]=0
            else:
                ans+=a[i]
                b[i+1] = b[i]+b[i+1]-a[i]
    print(ans)
if __name__ == "__main__":
    solve()

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

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

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

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

Solution (Python):
n = int(input())
a_li = [int(i) for i in input().split()]
b_li = [int(i) for i in input().split()]

if b_li[0]<b_li[-1]:
    a_li = a_li[::-1]
    b_li = b_li[::-1]

ans = 0
for i in range(0,n):
    attack = min(a_li[i],b_li[i])
    attack2 = min(a_li[i+1],b_li[i]-min(a_li[i],b_li[i]))
    a_li[i]-=attack
    a_li[i+1]-=attack2
    b_li[i]-=(attack+attack2)
    ans+=(attack+attack2)

print(ans)

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

goukei=0
for i in range(N):
    
    if A[i]>=B[i]:
        goukei = goukei+B[i]

    elif A[i]+A[i+1]>=B[i]:
        A[i+1] = A[i+1] - (B[i]-A[i])
        goukei = goukei + B[i]

    else:
        goukei = goukei + A[i]+A[i+1]
        A[i+1] = 0


print(goukei)

Solution (Python):
n=int(input())
m=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=0

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

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
count = 0
if N == 1:
  count += min(A[0]+A[1], B[0])
  print(count)
  exit(0)
mid_idx = N // 2
for idx in range(0, mid_idx):
  n_kill = min(A[idx], B[idx])
  count += n_kill
  A[idx] -= n_kill
  B[idx] -= n_kill
  n_kill = min(A[idx+1], B[idx])
  count += n_kill
  A[idx+1] -= n_kill
  B[idx] -= n_kill
  idx = -(idx+1)
  n_kill = min(A[idx], B[idx])
  count += n_kill
  A[idx] -= n_kill
  B[idx] -= n_kill
  n_kill = min(A[idx-1], B[idx])
  count += n_kill
  A[idx-1] -= n_kill
  B[idx] -= n_kill
if N % 2 != 0:
  count += min(A[mid_idx]+A[mid_idx+1], B[mid_idx])
print(count)

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

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

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

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
A=nl()
B=nl()
sumMb=sum(A)

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

print(sumMb-sum(A))

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

Solution (Python):
n = int(input())
a_ls = list(map(int, input().split()))
b_ls = list(map(int, input().split()))
before = sum(a_ls)

for i in range(n):
    target = a_ls[i:i+2]
    power = b_ls[i]
    if target[0] - power < 0:
        power -= target[0]
        target[0] = 0
        target[1] = max(0, target[1] - power)
    else:
        target[0] -= power
    a_ls[i:i+2] = target

after = sum(a_ls)
print(before - after)

Solution (Python):
import sys
input = sys.stdin.readline
import math
import collections
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n=I()
As=(LI())
Bs=(LI())
ans=0


for i in range(n):
    ans+=min(As[n-i]+As[n-1-i],Bs[n-1-i])
    As[n-i-1]=max(As[n-i-1]-max(0,Bs[n-1-i]-As[n-i]),0)
    
    
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):
    min_ = min(A[i], B[i])
    A[i] -= min_
    B[i] -= min_
    ans += min_
    min_ = min(A[i+1], B[i])
    A[i+1] -= min_
    B[i] -= min_
    ans += min_


print(ans)

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

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

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

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

new_sumA = sum(A)

print(sumA - new_sumA)

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

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

Asum = sum(A)

for i in range(N):
    a = A[i]
    b = B[i]
    A[i] = max(a - b, 0)
    B[i] = max(b - a, 0)
    A[i+1] = max(A[i+1]-B[i], 0)

print(Asum - sum(A))

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

dp = [0]*N 

for i in range(N):
  dp[i] = min(A[i], B[i])
  A[i] -= dp[i]
  B[i] -= dp[i]
  rem = A[i]
  if rem==0:
    tmp = min(A[i+1], B[i])
    dp[i] += tmp
    A[i+1] -= tmp
print(sum(dp))

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

Asum = sum(A)

for i in range(N):
    a = A[i]
    b = B[i]
    A[i] = max(a - b, 0)
    B[i] = max(b - a, 0)
    A[i+1] = max(A[i+1]-B[i], 0)

print(Asum - sum(A))

Solution (Python):
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))

n = load()
A = load_list()
B = load_list()

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

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

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
sm = sum(a)
for i in range(n):
    tmp = a[i]
    a[i] = max(a[i]-b[i], 0)
    b[i] -= tmp - a[i]
    a[i+1] = max(a[i+1]-b[i], 0)

print(sm - sum(a))

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

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

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

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

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

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

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

Solution (Python):
heros = int(input())
monsters = list(map(int, input().split()))
capacity = list(map(int, input().split()))
killed = 0
for i in range(heros):
    left = min(monsters[i], capacity[i])
    killed += left
    capacity[i] -= left
    if capacity[i] > 0:
        left2 = min(monsters[i+1], capacity[i])
        killed += left2
        monsters[i+1] -= left2
print(killed)

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

Solution (Python):
n = int(input())
ns = list(map(int, input().split()))
bs = list(map(int, input().split()))
count = 0
for i in range(n):
    monster = min(ns[i], bs[i])
    count += monster
    bs[i] -= monster
    monster2 = min(ns[i+1], bs[i])
    count += monster2
    ns[i+1] -= monster2
print(count)

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    N = int(input())
    A = input_nums()[::-1]
    B = input_nums()[::-1]
    B = [0] + B
    total = 0
    for i in range(N):
        
        mb = min(A[i], B[i])
        A[i] -= mb
        B[i] -= mb
        total += mb
        
        if A[i] > 0:
            mb = min(A[i], B[i+1])
            A[i] -= mb
            B[i+1] -= mb
            total += mb
    mb = min(A[N], B[N])
    total += mb
    print(total)

if __name__ == '__main__':
    main()

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

Solution (Python):
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect




from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7

def main():
  N = int(readline())
  A = list(map(int, readline().split()))
  B = list(map(int, readline().split()))
  count = 0
  for i in range(N):
    tmp = min(A[i],B[i])
    count += tmp
    A[i] -= tmp
    B[i] -= tmp
    tmp = min(A[i+1],B[i])
    count += tmp
    A[i+1] -= tmp
    B[i] -= tmp
  print(count)
  
  
if __name__ == '__main__':
  main()

Solution (Python):
city_count = int( input() )
monster_list = list( map( int, input().split() ) )
hero_power_list = list( map( int, input().split() ) )

before_total_monsters = sum( monster_list )

for i in range( city_count ):
  conflict = min( monster_list[i], hero_power_list[ i ] )
  monster_list[i] -= conflict
  hero_power_list[ i ] -= conflict
  conflict = min( monster_list[ i + 1 ], hero_power_list[ i ] )
  monster_list[ i + 1 ] -= conflict
  hero_power_list[ i ] -= conflict
monster_list[city_count] -= min( monster_list[city_count], hero_power_list[city_count-1] )

after_total_monsters = sum( monster_list )

print( before_total_monsters - after_total_monsters )

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

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


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

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

al=list(map(int,input().split()))
bl=list(map(int,input().split()))

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

print(ans)

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

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

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

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

ans = 0
for i in range(n-1, -1,-1):
    if(a[i+1] >= b[i]):
        ans += b[i]
        continue
    ans += a[i+1]
    b[i] -= a[i+1]
    tmp = min(a[i], b[i])
    ans += tmp
    a[i] -= tmp

print(ans)

Solution (Python):
N = int(input())
mons = list(map(int, input().split()))
yush = list(map(int, input().split()))

temp = sum(mons)

for i, yu in enumerate(yush):
    mo = mons[i]
    if (yu >= mo):
        mons[i] = 0
        yush[i] = yu - mo
        
        if (mons[i + 1] > yush[i]):
            mons[i + 1] -= yush[i]
        else:
            mons[i + 1] = 0
        
    else:
        mons[i] = mo - yu
ans = temp - sum(mons)
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):
    atk1 = min(A[i], B[i])
    atk2 = min(A[i + 1], B[i] - atk1)
    A[i + 1] -= atk2
    ans += atk1 + atk2

print(ans)