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

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

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

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

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


if __name__ == '__main__':
    main()

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

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

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

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

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

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

print(sm - sum(B))

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

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

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

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

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

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

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

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

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

print(fenum - sum(A))

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

for i in range(n):
    if A[i] > B[i]:
        A[i] -= B[i]
    else:
        B[i] -= A[i]
        A[i] = 0
        A[i + 1] = max(0, A[i + 1] - B[i])
after = sum(A)

answer = before - after
print(answer)

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

print(ans)

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

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

    for i in range(N):
        if a[i] >= b[i]:
            ans += b[i]
        else:
            if a[i+1] >= (b[i] - a[i]):
                ans += b[i]
                a[i+1] = a[i+1] - (b[i] - a[i])
            else:
                ans += (a[i+1]+a[i])
                a[i+1] = 0
    return int(ans)
if __name__ == '__main__':
    print(main())

Solution (Python):
n = int(input())
ls_a = list(map(int, input().split()))
ls_b = list(map(int, input().split()))

ans = 0
res = 0

for i in range(n):
	ans += min(ls_a[i], res + ls_b[i])
	if res >= ls_a[i]:
		res = ls_b[i]
	else:
		res = max(res + ls_b[i] - ls_a[i], 0)

if res >= ls_a[n]:
	print(ans + ls_a[n])
elif 0 <= res < ls_a[n]:
	print(ans + res)
else:
	print(ans)

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

ans0 = 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
    elif b[i] <= a[i]:
        a[i] -= b[i]
ans1 = sum(a)
print(ans0-ans1)

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

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

    count = 0
    for i in range(n):
        
        
        if a_list[i] >= b_list[i]:
            a_list[i] -= b_list[i]
            count += b_list[i]
            b_list[i] = 0
        else:
            b_list[i] -= a_list[i]
            count += a_list[i]
            a_list[i] = 0

        

        if a_list[i+1] >= b_list[i]:
            a_list[i+1] -= b_list[i]
            count += b_list[i]
            b_list[i] = 0
        else:
            b_list[i] -= a_list[i+1]
            count += a_list[i+1]
            a_list[i+1] = 0

        

    print(count)


if __name__ == '__main__':
    main()

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

enemy = sum(a)
for i in range(len(b)-1, -1, -1):
    tmp = max(a[i + 1] - b[i], 0)
    b[i] -= a[i + 1] - tmp
    a[i + 1] = tmp
    tmp = max(a[i] - b[i], 0)
    b[i] -= a[i] - tmp
    a[i] = tmp
ans = enemy - sum(a)
print(ans)

Solution (Python):
import numpy as np
n = int(input())
a0 = np.array(list(map(int, input().split())))
a = a0.copy()
b = np.array(list(map(int, input().split())))
for i in range(n):
  c = a[i] - b[i]
  if c < 0:
    a[i] = 0
    a[i+1] = a[i+1] + c
    if a[i+1] < 0:
      a[i+1] = 0
  else:
    a[i] = a[i] - b[i]
print(sum(a0 - a))

Solution (Python):
N = int(input())
town = list(map(int,input().split(" ")))
town_before = sum(town)
brave = list(map(int,input().split(" ")))
for i in range(N):
    if town[i] < brave[i]:
        brave[i] -= town[i]
        town[i] = 0
        if town[i + 1] < brave[i]:
            town[i + 1] = 0
        else:
            town[i + 1] -= brave[i]
    else:
        town[i] -= brave[i]
print(town_before - sum(town))

Solution (Python):


import math
import sys
import heapq
import fractions
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]:
        if B[i] >= A[i+1]+A[i]:
            ans += A[i]+A[i+1]
            A[i+1] = 0
        else:
            A[i+1] -= B[i]-A[i]
            ans += B[i]
    else:
        ans += B[i]

print(ans)

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

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

for i in range(n):
    if i != n-1:
        if l2[i] >= l1[i]:
            c += l1[i]
            diff = l2[i] - l1[i]
            l2[i+1] += min(diff, l1[i+1])
        else:
            c += l2[i]
    else:
        if l2[i] >= l1[i] + l1[i+1]:
            c += l1[i] + l1[i+1]
        else:
            c += l2[i]

print(c)

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

z = 0
ans = 0
for a, b in zip(A, B):
    ans += min(a, b + z)
    z = max(0, b - max(0, a - z))

print(ans)

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

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

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

def calc(a, b, c):
    if a < b:
        b -= a 
        c += a 
        a = 0 
    else:
        a -= b 
        c += b 
        b = 0
    return a, b, c 

c = 0
for i in reversed(range(N)):
    A[i+1], B[i], c = calc(A[i+1], B[i], c)
    A[i], B[i], c = calc(A[i], B[i], c)

print(c)

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

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

s = sum(B)
for i in range(n):
    diff = min(A[i], B[i])
    A[i] -= diff
    B[i] -= diff
    diff_next = min(A[i+1], B[i])
    A[i+1] -= diff_next
    B[i] -= diff_next

print(s - sum(B))

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

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

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

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

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

print(monster)

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

count = 0

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

    count += kill

print(count)

Solution (Python):

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

count = 0
for i in range(n):
    if a_list[i] <= b_list[i]: 
        
        yoryoku = b_list[i] - a_list[i]
        if yoryoku < a_list[i+1]: 
            count += a_list[i] + yoryoku
            a_list[i+1] -= yoryoku
            a_list[i] = 0
        else: 
            count += a_list[i] + a_list[i+1]
            a_list[i + 1] = 0
            a_list[i] = 0
    else:  
        count += b_list[i]
print(count)

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

Solution (Python):
n = int(input())
monster = list(map(int, input().split()))
yusya = list(map(int, input().split()))
prev = 0
res = 0
for i in range(n):
  if yusya[i] <= monster[i]:
    res += yusya[i]
    continue
  else:
    res += monster[i]
    yusya[i] -= monster[i]
    if yusya[i] <= monster[i+1]:
      monster[i+1] -= yusya[i]
      res += yusya[i]
    else:
      res += monster[i+1]
      monster[i+1] = 0
print(res)

Solution (Python):


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
l = A[0]
for i in range(N):
    r = A[i + 1]
    cur = B[i]
    
    
    

    
    ans += min(l, cur)
    rest_power = max(cur - l, 0)
    ans += min(rest_power, r)
    l = max(r - rest_power, 0)  

print(ans)

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

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

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

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

Solution (Python):
import sys
import itertools
import bisect
import math

stdin = sys.stdin

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

n = ni()
a = na()
b = na()

ans = 0

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

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

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

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

            aj = a[i+1]
            if aj >= bi:
                a[i+1] -= bi
                ans += bi
            else:
                a[i+1] = 0
                ans += aj
    print(ans)

if __name__ == '__main__':
    main()

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, 0, -1):
    tmpA = A[i]
    tmpB = B[i-1]
    A[i] = max(0, tmpA-tmpB)
    B[i-1] = max(0, tmpB-tmpA)
    tmpA = A[i-1]
    tmpB = B[i-1]
    A[i-1] = max(0, tmpA-tmpB)
    B[i-1] = max(0, tmpB-tmpA)
print(sumA-sum(A))

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

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

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

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

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

            aj = a[i+1]
            if aj >= bi:
                a[i+1] -= bi
                ans += bi
            else:
                a[i+1] = 0
                ans += aj
    print(ans)

if __name__ == '__main__':
    main()

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

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
s = sum(A)
B = [int(i)for i in input().split()]
for i,b in enumerate(B):
    if b > A[i]:
        b -= max(0,A[i])
        A[i] = 0
        A[i+1] -= b
    else:
        A[i] -= b
A[-1] = max(0,A[-1])
print(s-sum(A))

Solution (Python):

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

def kill(saver, next, curr):
	killed = 0
	if saver >= next:
		saver -= next
		killed += next
		if saver >= curr:
			killed += curr
			return 0, killed
		else:
			killed += saver
			return curr-saver, killed 
	else:
		killed += saver
		return curr, killed

cnt = 0
res = A[-1]
for i in range(len(B)):
	ans = kill(B[len(B)-i-1], res, A[len(A)-i-2])
	res = ans[0]
	cnt += ans[1]
print(cnt)

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

print(cnt)

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

ans = 0
for i in range(N):

    if B[i] >= A[i]:
        ans += 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

    else:
        ans += B[i]

print(ans)

Solution (Python):

import sys


def solve(N: int, A: "List[int]", B: "List[int]"):
    answer = 0

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

            if A[i+1]>=B[i]:
                answer += B[i]
                A[i+1]-=B[i]
            else:
                answer += A[i+1]
                A[i+1]=0
    print(answer)
        
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N + 1)]  
    B = [int(next(tokens)) for _ in range(N)]  
    solve(N, A, B)

if __name__ == '__main__':
    main()

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

ans = 0

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

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

ans=0
rem=A[0]

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

Solution (Python):
import sys

def main():
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    ans=0;
    for i in range(n):
        kl=min(a[i],b[i])
        b[i]-=kl
        ans+=kl
        kl=min(a[i+1],b[i])
        a[i+1]-=kl
        ans+=kl
    print(ans)
    return 0

if __name__ == "__main__":
    sys.exit(main())

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N = I()
    a = LI()
    b = LI()
    cnt = 0
    for i in range(len(b)):
        left = min(a[i], b[i])
        cnt += left
        b[i] -= left

        right = min(a[i+1], b[i])
        cnt += right
        b[i] -= right
        a[i+1] -= right
    print(cnt)
    """
    a = [0] + a
    b = [0] + b + [0]
    cnt = 0
    for i in range(1, N+2):
        if a[i] - b[i-1] >= 0:
            cnt += b[i-1]
            a[i] = a[i] - b[i-1]
            b[i-1] = 0
        else:
            cnt += a[i]
            b[i-1] = b[i-1] - a[i]
            a[i] = 0

        if a[i] - b[i] >= 0:
            cnt += b[i]
            a[i] = a[i] - b[i]
            b[i] = 0

        else:
            cnt += a[i]
            b[i] = b[i] - a[i]
            a[i] = 0
    print(cnt)
    """

main()

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N = I()
    a = LI()
    b = LI()
    a = [0] + a
    b = [0] + b + [0]
    cnt = 0
    for i in range(1, N+2):
        if a[i] - b[i-1] >= 0:
            cnt += b[i-1]
            a[i] = a[i] - b[i-1]
            b[i-1] = 0
        else:
            cnt += a[i]
            b[i-1] = b[i-1] - a[i]
            a[i] = 0

        if a[i] - b[i] >= 0:
            cnt += b[i]
            a[i] = a[i] - b[i]
            b[i] = 0

        else:
            cnt += a[i]
            b[i] = b[i] - a[i]
            a[i] = 0
    print(cnt)

main()

Solution (Python):
from __future__ import print_function

import sys
input = sys.stdin.readline

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return















































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

    cnt=0;i=0
    while i<n:
        if b[i]==0 or (a[i]==0 and a[i+1]==0):
            i+=1
            
        else:
            temp=min(a[i],b[i]);a[i]-=temp;b[i]-=temp;cnt+=temp
            if b[i]==0:
                pass
            else:
                temp=min(b[i],a[i+1]);a[i+1]-=temp;b[i]-=temp;cnt+=temp
    print(cnt)
    
        
    return

if __name__ == '__main__':
    main()
'''
for given
n (1<=n<=10**5),
[a_i] (\forall i in [0...(n-1)].1<=a_i<=10**9 ),
[b_i] (\forall i in [0...(n-1)].1<=b_i<=10**9 )

find x_cnt such that
x_cnt:= sum(\for i in [0...(n-1)].b[i]    barahbarahbarah)


(a[i] + a[i+1] <= b[i] then )
min(a[i] + a[i+1] , b[i])

\forall i in [0...(n-1)]. a[i] - c[i]

'''

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

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

print(hunted_monsters)

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

print(ans)

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

    total = np.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

        elif b[i] < a[i]:
            a[i] -= b[i]

    print(total-np.sum(a))

    
    
if __name__ == '__main__':
    main()

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

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]
        b[i] = 0
    elif a[i] == b[i]:
        count += a[i]
        b[i] = 0
    else:
        count += a[i]
        b[i] -= a[i]
        if a[i+1] > b[i]:
            count += b[i]
            a[i+1] -= b[i]
            b[i] = 0
        elif a[i+1] == b[i]:
            count += a[i+1]
            a[i+1] = 0
            b[i] = 0
        else:
            count += 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()))
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):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
cnt = 0
for i in range(n):
    if a[i] >= b[i]:
        cnt += b[i]
    else:
        cnt += a[i]
        if a[i+1] > b[i]-a[i]:
            a[i+1] -= (b[i] - a[i])
            cnt += b[i]-a[i]
        else:
            cnt += a[i+1]
            a[i+1] = 0
print(cnt)

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

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

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

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

print(buster)