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
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
t=0
y=[0]

for i in range(n):

    if a[i]-y[i] >= b[i]:
        
        t += b[i]
        y.append(0)
    
    elif a[i]-y[i]+a[i+1] <= b[i]:
        t += a[i]-y[i]+a[i+1]
        y.append(a[i+1])
    else:
        t += b[i]
        y.append(b[i]-a[i]+y[i])

print(t)

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

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

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

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

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

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

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

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

print(ans)

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

A = list(map(int, input().split()))
B = list(map(int, input().split()))
B.append(0)
C = [0] * (N + 2)

cnt = 0

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

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

print(total)

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

mons = 0
b0 = 0

for a, b in zip(al, bl):
    
    if a >= (b + b0):
        mons += (b + b0)
        b0 = 0
    else:
        mons += a

        
        

        b0 = b - max(0, a-b0)

        
    

print(mons)

Solution (Python):
import sys
from collections import Counter
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-1, -1, -1):
    remain = B[i]
    if remain <= A[i+1]:
        ans += remain
        remain = 0
    else:
        ans += A[i+1]
        remain -= A[i+1]
    if remain <= A[i]:
        ans += remain
        A[i] -= remain
    else:
        ans += A[i]
        A[i] = 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 a[i] >= c[i]:
        ans += c[i]
        a[i] -= c[i]
    else:
        ans += a[i]
        a[i] = 0
        
    if a[i] >= b[i]:
        ans += b[i]
    else:
        ans += a[i]
        c[i+1] = b[i]-a[i]
if a[n] >= c[n]:
    ans += c[n]
else:
    ans += a[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):
    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 b[i] > a[i] + a[i+1]:
          ans += a[i] + a[i+1]
          a[i+1] = 0
     elif a[i] <= b[i] <=a[i] + a[i+1]:
          ans += b[i]
          a[i+1] = a[i] + a[i+1] - b[i]
     else:
          ans += b[i]
     
print(ans)

Solution (Python):
def solve():
    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]
            rest = B[i] - A[i]
            if A[i+1] > rest:
                cnt += rest
                A[i+1] -= rest
            else:
                cnt += A[i+1]
                A[i+1] = 0
    
    print(cnt)

if __name__ == '__main__':
    solve()

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
m=sum(B)
for i in range(n):
    c=min(A[i],B[i])
    A[i]-=c
    B[i]-=c
    d=min(A[i+1],B[i])
    A[i+1]-=d
    B[i]-=d
m-=sum(B)
print(m)

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

monster_num=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(monster_num-sum(A))

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

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

print(killed)

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

Solution (Python):
n = int(input())
monster = list(map(int, input().split()))
hero = list(map(int, input().split()))

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

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

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

Solution (Python):
n=int(input())
a=[0]*(n+1)
b=[0]*n
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]
    b[i]=0
    a[i]-=b[i]
  else:
    ans+=min(b[i],a[i]+a[i+1])
    a[i+1]-=b[i]-a[i]
    a[i]=0
    a[i+1]=max(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):
    x=min(a[i],b[i])
    a[i]-=x
    b[i]-=x
    ans+=x
    y=min(a[i+1],b[i])
    a[i+1]-=y
    b[i]-=y
    ans+=y
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]
        if B[i] - A[i] > A[i + 1]:
            ans += A[i + 1]
            A[i + 1] = 0
        else:
            ans += B[i] - A[i]
            A[i + 1] -= B[i] - A[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):
  
  tmp = min(A[i], B[i])
  ans += tmp
  A[i] -= tmp
  B[i] -= tmp
  
  
  tmp2 = min(A[i+1],B[i])
  ans += tmp2
  A[i+1] -= tmp2
  B[i] -= tmp2
print(ans)

Solution (Python):

"""
Created on Wed Mar 25 21:14:23 2020

@author: Kanaru Sato
"""

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

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

print(sum)

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

Solution (Python):
n = int(input())
alst = [int(i) for i in input().split()]
blst = [int(i) for i in input().split()] + [0]
n_monster = 0
next_capa = 0
for a, b in zip(alst, blst):
	n_monster += min(a, b + next_capa)
	next_capa = max(b - max(a - next_capa, 0), 0)
print (n_monster)

Solution (Python):
def LI():
	return [ int(s) for s in input().split() ]
  
N = int(input())
A = LI()
B = LI()

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

print(cnt)

Solution (Python):
N = int(input())
A = [int(s) for s in input().split(' ')]
B = [int(s) for s in input().split(' ')] + [0]
m = 0
pa = 0
for a, b in zip(A, B):
    m1 = 0
    if pa > 0:
        m1 = min(pa, a)
        a -= m1
    m2 = min(a, b)
    pa = b - m2
    m += m1 + m2

print(m)

Solution (Python):
import os, sys, re, math

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

total = 0
for i in range(N, 0, -1):
    for j in [0, 1]:
        if A[i - j] and B[i - 1]:
            count = min(A[i - j], B[i - 1])
            A[i - j] -= count
            B[i - 1] -= count
            total += count

print(total)

Solution (Python):
n = int(input())
monsters = list(map(int, input().split(' ')))
kills = list(map(int, input().split(' ')))
monsters_orig = sum(monsters)
 
for i in range(n):
    extra_attack = kills[i] - monsters[i]
    monsters[i] = max(0, monsters[i] - kills[i])
    if extra_attack > 0:
        monsters[i+1] = max(0, monsters[i+1] - extra_attack)
    
print(monsters_orig - sum(monsters))

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


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

print(sum)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
tot = sum(A)
for i in range(n):
    en = A[i] + A[i+1]
    A[i] = max(A[i] -B[i],0)
    if A[i] == 0:
        A[i+1] = max(en - B[i],0)
        
print(tot-sum(A))

Solution (Python):

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
count = 0
tmp = 0
for i in range(n):
    tmp = min(a[i] + a[i + 1], b[i])
    count += tmp
    a[i + 1] -= max(0, tmp - a[i])
print(count)

Solution (Python):
def main():
    from builtins import int, map, list, print, min
    import sys
    sys.setrecursionlimit(10 ** 6)

    input = sys.stdin.readline
    input_list = (lambda: input().rstrip().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from builtins import int, map, list, print, min
    import sys
    sys.setrecursionlimit(10 ** 6)

    input = sys.stdin.readline
    input_list = (lambda: input().rstrip().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

total = 0
for i in range(n):
    temp = min(aa[i], bb[i])
    total += temp
    aa[i] -= temp
    if aa[i] < bb[i]:
        temp_n = min(bb[i]-temp, aa[i+1])
        total += temp_n
        aa[i+1] -= temp_n
print(total)

Solution (Python):

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


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

print(sum)

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

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

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

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

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

Solution (Python):

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

Solution (Python):

import sys
import itertools


from operator import itemgetter
from sys import stdin


N=3
M=3
i=0
j=0
k=0
ans=0


N=int(input())



monster_list=list(map(int, input().split(" ")))
beat_list=list(map(int, input().split(" ")))



for i in reversed(range(N+1)):
 
 
 
 if i >=1:
    if beat_list[i-1]<monster_list[i]:
        ans=ans+beat_list[i-1]
        beat_list[i-1]=0
        monster_list[i]=monster_list[i]-beat_list[i-1]
    else:
        beat_list[i-1]=beat_list[i-1]-monster_list[i]
        ans=ans+monster_list[i]
        monster_list[i]=0
        if beat_list[i-1]>=monster_list[i-1]:
            ans=ans+monster_list[i-1]
            monster_list[i-1]=0
        else:
            ans=ans+beat_list[i-1]
            monster_list[i-1]=monster_list[i-1]-beat_list[i-1]
            beat_list[i-1]=0

 
 
 

 
 
print(ans)

"1行1つの整数を入力を取得し、整数と取得する"




"12 21 332 とか入力する時に使う"
"1行に複数の整数の入力を取得し、整数として扱う"

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

for i in number_list:
    print(number_list[i-1])

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''
x=[]
y=[]
for i in range(N):
    x1, y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
print(x)
print(y)
"複数行に2数値を入力する形式 x座標とy座標を入力するイメージ"
'''

'''
mixlist=[]
for i in range(N):
    a,b=input().split()
    mixlist.append((int(a),b))
print(mixlist)
"複数行にintとstrを複合して入力するやつ，今回はリスト一つで処理している"


とかに使う
'''

'''



"正方行列にstr型の値を入力"
'''




'''
入力
1 2
4 5
7 8
出力結果
[[1, 2], [4, 5], [7, 8]]
'''
"列数に関して自由度の高いint型の値を入力するタイプの行列"


'''
table = [[int(i) for i in input().split()] for m in range(m)]

print(type(N))
print(N)
print(type(M))
print(M)

print(type(table))
print(table)
'''











'''
入力
1234
出力
1
[[1, 2, 3, 4]]
'''




'''
word_list= input().split(" ")
print(word_list[0])
"連続文字列の入力"
"qw er ty とかの入力に使う"
"入力すると空白で区切ったところでlistの番号が与えられる"
'''



'''
A, B, C=stdin.readline().rstrip().split()
print(A)
"リストではなく独立したstr型を入れるなら以下のやり方でOK"
'''




"aという変数に入っているものを大文字にして出力"

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

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

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

Solution (Python):
import sys

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

N = ir()
A = lr()
B = lr()
answer = 0
for i in range(N):
    a, b = A[i], B[i]
    answer += min(a, b)
    if a < b:
        remain = b - a
        add = min(A[i+1], remain)
        answer += add
        A[i+1] -= add

print(answer)

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

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

Solution (Python):
n = int(input())
a_lst = [int(elem) for elem in input().split()]
b_lst = [int(elem) for elem in input().split()]

res = 0
for i in range(n+1):
  if i == 0:
    res += min(a_lst[i], b_lst[i])
    if b_lst[i] > a_lst[i]:
      b_lst[i] -= a_lst[i]
    else:
      b_lst[i] = 0
  elif i == n:
    res += min(a_lst[i], b_lst[i-1])
    if b_lst[i-1] > a_lst[i]:
      b_lst[i-1] -= a_lst[i]
    else:
      b_lst[i-1] = 0    
  else:
    res += min((b_lst[i-1] + b_lst[i]), a_lst[i])
    if b_lst[i-1] > a_lst[i]:
      b_lst[i-1] -= a_lst[i]
    elif b_lst[i] > (a_lst[i] - b_lst[i-1]):
      b_lst[i] -= (a_lst[i] - b_lst[i-1])
      b_lst[i-1] = 0
    else:
      b_lst[i] = 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):
    if A[i] >= B[i]:
        ans += B[i]
        A[i] -= B[i]
    else:
        ans += A[i]
        B[i] -= A[i]
        ans += min(A[i+1], B[i])
        A[i+1] = max(0, A[i+1]-B[i])

print(ans)

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

sum_A = sum(A)

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

ans = sum_A - sum(A)
print(ans)

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

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

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

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

cnt = 0
for i in range(N):
    l =min(A[i],B[i])
    
    B[i] -= l                       
    cnt += l
    if B[i] > 0:                    
        r = min(A[i+1],B[i])
        A[i+1] -= r                 
        
        cnt += r
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):
    l =min(A[i],B[i])
    
    B[i] -= l                       
    cnt += l
    if B[i] > 0:                    
        r = min(A[i+1],B[i])
        A[i+1] -= r                 
        
        cnt += r
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):
    l =min(A[i],B[i])
    
    B[i] -= l                       
    cnt += l
    if B[i] > 0:                    
        r = min(A[i+1],B[i])
        A[i+1] -= r                 
        B[i] -= r
        cnt += r
print(cnt)

Solution (Python):
import sys
from heapq import heappush, heappop


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

if __name__ == '__main__':
    main()

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

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

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

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

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

print(res)

Solution (Python):
f=lambda:list(map(int,input().split()))
n=f()[0];l=f();c=0;t=l.pop(0)
for a,b in zip(l,f()):
  s=min(t+a,b);t=min(max(t+a-s,0),a);c+=s
print(c)

Solution (Python):
f=lambda:list(map(int,input().split()))
n=f()[0];l=f();c,t=0,l[0]
for a,b in zip(l[1:],f()):
  s=min(t+a,b);t=min(max(t+a-s,0),a);c+=s
print(c)

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

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

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

print(count)

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
ans = 0
for i in range(N):
    remain_power = B[i] - A[i]
    if remain_power <= 0:
        ans += B[i]
    else:
        ans += A[i]
        next_A = max(0, A[i+1] - remain_power)
        ans += A[i+1] - next_A
        A[i+1] = next_A
print(ans)

Solution (Python):
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

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

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

print(sm - sum(A))