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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = B*1
B.append(0)
B.append(0)

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

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

ans = 0

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

print(ans)

Solution (Python):
n=int(input())
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]
    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
  else:
    ans+=b[i]
print(ans)

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

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

    res = 0
    for i in range(n):
        tmp = min(a[i],b[i])
        res += tmp
        a[i] -= max(tmp,0)
        if b[i] > tmp:
            tmp = min(a[i+1],b[i]-tmp)
            res += tmp
            a[i+1] -= max(tmp,0)
    print(res)
 
if __name__ == '__main__':
    main()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

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

            if A[i + 1] <= B[i]:
                res += A[i + 1]
                B[i] -= A[i + 1]
                A[i + 1] = 0
            else:
                res += B[i]
                A[i + 1] -= B[i]
                B[i] = 0
        else:
            res += B[i]
            A[i] -= B[i]
            B[i] = 0

    print(res)


if __name__ == '__main__':
    resolve()

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

if __name__ == "__main__":
  resolve()

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

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

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

print(total_a - sum(a))

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

ans = 0
pre = a[0]
for ai, bi in zip(a[1:], b):
    if pre <= bi:
        ans += pre
        bi -= pre
    else:
        ans += bi
        bi = 0
    pre = ai
    if pre <= bi:
        ans += pre
        pre = 0
    else:
        ans += bi
        pre -= bi
print(ans)

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

count = 0
for idx, b in enumerate(B):
    count += min(b, A[idx])
    rest = min(A[idx + 1], b - min(b, A[idx]))
    A[idx + 1] -= rest
    count += rest
print(count)

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






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N = I()
A = IL()
A_copy = A+[]
B = IL()

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

P(ans)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N = I()
A = IL()
A_copy = A+[]
B = IL()

ans1 = 0
ans2 = 0

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

print(max(ans1,ans2))

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

for i in range(N+1):
  sa = min(A[i],B[i-1])
  A[i]-=sa
  B[i-1]-=sa
  ans+=sa
  
  sa = min(A[i],B[i])
  A[i]-=sa
  B[i]-=sa
  ans+=sa
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):
    for j in range(i,i+2):
        dif = b[i] - a[j]
        if dif<0:
            ans+= b[i]
            b[i]=0
            a[j]=-dif
        elif dif >=0:
            ans+= a[j]
            b[i] = dif
            a[j] = 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 b[i]<=a[i]:
    a[i]-=b[i]
    ans+=b[i]
  elif b[i]<=(a[i]+a[i+1]):
    a[i+1]-=(b[i]-a[i])
    a[i]=0
    ans+=b[i]
  else:
    ans+=(a[i]+a[i+1])
    a[i]=0
    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):
    t = min(b,A[i]+A[i+1])
    ans += t
    
    A[i+1] -= max(0, t-A[i])
    
print(ans)

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

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

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

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

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

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

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

print(count)

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

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

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

Solution (Python):
def exception_handling(n, ali, bli):

    bli.append(0)
    count = 0
    ans = 0

    for i in range(n):
        count = min(ali[i], bli[i])
        ans += count
        ali[i] -= count
        bli[i] -= count

        count = min(ali[i+1], bli[i])
        ans += count
        ali[i+1] -= count
        bli[i] -= count

    return ans

def main():
    n = int(input())
    ali = list(map(int, input().split()))
    bli = list(map(int, input().split()))
    print(exception_handling(n, ali, bli))

if __name__ == '__main__':
    main()

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


resolve()

Solution (Python):

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



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

print(kill_monsters)

Solution (Python):
n = int(input())
lia = list(map(int,input().split()))
lib = list(map(int,input().split()))
mon = sum(lia)
for i in range(n):
    k = lia[i] - lib[i]
    if k <= 0:
        lia[i] = 0
        lia[i+1] += k
        if lia[i+1] < 0:
            lia[i+1] = 0
    else:
        lia[i] -= lib[i]
print(mon-sum(lia))

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

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

As[0], Bs[0], ans = max(As[0]-Bs[0], 0), max(Bs[0]-As[0], 0), min(As[0], Bs[0])

for i in range(1,N):
  As[i], Bs[i-1], ans = max(As[i]-Bs[i-1], 0), max(Bs[i-1]-As[i], 0), ans + min(As[i], Bs[i-1])
  if As[i] > 0:
    As[i], Bs[i], ans = max(As[i]-Bs[i], 0), max(Bs[i]-As[i], 0), ans + min(As[i], Bs[i])

if As[-1] > 0 and Bs[-1] > 0:
  ans += min(As[-1], Bs[-1])
    
print(ans)

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

for i in range(len(b)):
  a[i] -= b[i]
  if(a[i] < 0):
    num = a[i] * (-1)
    a[i + 1] -= num
    a[i] = 0
    if(a[i + 1] < 0):
      a[i + 1] = 0

print(s - sum(a))

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import copy
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

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

N = getN()
A = getList()
B = getList()
cnt = 0
for i in range(N):
    minus1 = min(A[-i - 1], B[-i - 1])
    A[-i - 1] -= minus1
    B[-i - 1] -= minus1
    cnt += minus1
    minus2 = min(A[-i - 2], B[-i - 1])
    A[-i - 2] -= minus2
    B[-i - 1] -= minus2
    cnt += minus2
print(cnt)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
prev = a[0]
total = 0
for i, j in zip(a[1:], b):
    if prev >= j:
        total += j
        prev = i
    else:
        task = prev + i
        if task <= j:
            total += task
            prev = 0
        else:
            total += j
            prev = task - j
print(total)

Solution (Python):


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

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

Solution (Python):
n = int(input())
an = list(map(int,input().split()))
bn = list(map(int,input().split()))
nokori = 0
mon = 0
for i in range(n+1):
    if i != n:
        a = an[i]
        if a<=nokori:
            mon += a
            nokori = bn[i]
        else:
            mon += nokori
            a -= nokori
            if a <= bn[i]:
                mon += a
                nokori = bn[i]-a
            else:
                mon += bn[i]
                nokori = 0
    else:
        a = an[i]
        mon += min(a,nokori)

print(mon)

Solution (Python):
N=int(input())
AList = list(map(int, input().split()))
BList = list(map(int, input().split()))

Asum=sum(AList)

i=0
while i<N:
    Amari=max(0,BList[i]-AList[i])
    AList[i]=max(AList[i]-BList[i],0)
    AList[i+1]=max(AList[i+1]-Amari,0)
    
    
    i+=1

print(Asum-sum(AList))

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

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

Solution (Python):
n=int(input())
a=[0]*(n+1)
b=[0]*n
a[:]=map(int,input().split())
b[:]=map(int,input().split())

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

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

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

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

Solution (Python):
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
geta = lambda fn: map(fn, readline().split())
gete = lambda fn: fn(readline())



def main():
    N = gete(int) 
    A = list(geta(int))
    B = list(geta(int))

    ans = 0
    for i, b in enumerate(B):
        if A[i] + A[i+1] < b:
            ans += A[i] + A[i+1]
            A[i+1] = 0
        else:
            ans += b 
            if b > A[i]:
                A[i+1] -= b - A[i]
    
    print(ans)
	
if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
i= list(map(int, input().split()))
j= list(map(int, input().split()))
j.append(0)
amari=0
ans=0
for k in range(n+1):
    s=i[k]
    i[k]-=amari
    if i[k]<0:
        i[k]=0
        ans+=s
    else:
        ans+=amari
    amari=0
    if i[k]<j[k]:
        amari+=(j[k]-i[k])
        ans+=i[k]
    else:
        ans+=j[k]
print(ans)

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

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

print(cnt)

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

Solution (Python):
import sys
def main():
  n, *lst = map(int, sys.stdin.read().split())
  a = lst[:n+1]
  b = lst[n+1:]
  res = 0
  for i in range(n):
    if i == 0:
      tmp = min(a[i], b[i])
      res += tmp
      b[i] -= tmp
    else:
      tmp = min(a[i], b[i] + b[i - 1])
      res += tmp
      b[i] -= min(b[i], max(tmp - b[i - 1], 0))
  res += min(a[n], b[n - 1])
  print(res)


if __name__ == '__main__':
  main()

Solution (Python):
import sys
def main():
  n, *lst = map(int, sys.stdin.read().split())
  a = lst[n::-1]
  b = lst[-1:n:-1]
  res = 0
  for i in range(n):
    if i == 0:
      tmp = min(b[i], a[i])
      res += tmp
      a[i] -= tmp
      b[i] -= tmp
    else:
      tmp = min(b[i] + b[i - 1], a[i])
      res += tmp
      a[i] -= tmp
      b[i] -= min(b[i], max(tmp - b[i-1], 0))
  res += min(b[n - 1], a[n - 1] + a[n])
  print(res)


if __name__ == '__main__':
  main()

Solution (Python):
import itertools
import math
import fractions
import functools
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
goke = sum(a)
for i in range(n):
    if b[i] > a[i]:
        a[i+1] -= (b[i]-a[i])
        if a[i+1] < 0:
            a[i+1] = 0
        a[i] = 0
    else:
        a[i] -= b[i]

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

print(ans)

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

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

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

    else:
        c += b[i]
print(c)

Solution (Python):
import numpy as np
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
A = np.array(A)
B = np.array(B)
A_org = A.copy()

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

ans = A_org-A
print(ans.sum())

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    la=list(map(int,input().split()))
    lb=list(map(int,input().split()))
    cnt=0
    for i in range(n):
        x=min(la[i],lb[i])
        cnt+=x
        lb[i]-=x
        la[i]-=x
        y=min(lb[i],la[i+1])
        cnt+=y
        la[i+1]-=y
    print(cnt)
resolve()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

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

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

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

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

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

print(taoshita)