Problem:
 Score : 300 points    Problem Statement There are N+1 towns. The i -th town is being attacked by A_i monsters.  We have N heroes. The i -th hero can defeat monsters attacking the i -th or (i+1) -th town, for a total of at most B_i monsters.  What is the maximum total number of monsters the heroes can cooperate to defeat?      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq A_i \leq 10^9  1 \leq B_i \leq 10^9         Input Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_{N+1}  B_1  B_2  ...  B_N       Output Print the maximum total number of monsters the heroes can defeat.        Sample Input 1 2
3 5 2
4 5      Sample Output 1 9  If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.   The first hero defeats two monsters attacking the first town and two monsters attacking the second town.  The second hero defeats three monsters attacking the second town and two monsters attacking the third town.        Sample Input 2 3
5 6 3 8
5 100 8      Sample Output 2 22       Sample Input 3 2
100 1 1
1 100      Sample Output 3 3

Solution (Python):
from sys import stdin, setrecursionlimit


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

    ans = 0
    tmp = 0

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

    if tmp >= a[-1]:
        ans += a[-1]
    else:
        ans += tmp

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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

ans = 0

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

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)

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 = A[i] - B[i]
    if tmp < 0 and -tmp <= A[i+1]:
        A[i+1] += tmp
        ans += B[i]
    elif tmp < 0 and -tmp > A[i+1]:
        ans += A[i] +A[i+1]
        A[i+1] = 0
    elif tmp >= 0:
        ans += B[i]

print(ans)

Solution (Python):
N, *AB = map(int, open(0).read().split())
A = AB[:N+1]
B = AB[N+1:]

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())
monster = list(input().split())
hero = list(input().split())
monster = [int(monster[i]) for i in range(N+1)]
hero = [int(hero[i]) for i in range(N)]

count = 0
for i in range(N):
    count += min(monster[i+1] + monster[i], hero[i])
    monster[i+1] = min(monster[i+1], max(monster[i+1] - (hero[i] - monster[i]), 0))

print(count)

Solution (Python):
def main():
    _ = input()
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    sum_prev = sum(A)

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

    print(sum_prev - sum(A))


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

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())
enemy = [int(i) for i in input().split()]
heros = [int(i) for i in input().split()]
defeat = 0


for i in range(n):
  if enemy[i] > heros[i]:
    defeat += heros[i]
  else:
    defeat += enemy[i]
    extra = heros[i] - enemy[i]
    if extra >= enemy[i+1]:
      defeat += enemy[i+1]
      enemy[i+1] = 0
    else:
      defeat += extra
      enemy[i+1] -= extra


print(defeat)

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

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

print(left)

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

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

print(kill)

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

sumA = sum(A)
for i in range(n-1,-1,-1):
    y = B[i]
    if(B[i] <= A[i+1]):
        A[i+1] -= B[i]
        B[i] = 0
    else:
        B[i] -= A[i+1]
        A[i+1] = 0
    
    if(B[i] == 0):
        continue
    if(B[i] <= A[i]):
        A[i] -= B[i]
        B[i] = 0
    else:
        B[i] -= A[i]
        A[i] = 0
subSumA = sum(A)

print(sumA - subSumA)

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

Solution (Python):


N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

count_defeat = 0

for i, b in enumerate(B_list):
    if A_list[i] >= b:
        count_defeat += b

    else:
        count_defeat += A_list[i]
        remain_b = (b - A_list[i])

        if remain_b > A_list[i + 1]:
            count_defeat += A_list[i + 1]
            A_list[i + 1] = 0

        else:
            count_defeat += remain_b
            A_list[i + 1] -= remain_b

print(count_defeat)

Solution (Python):
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    num = int(input())        
    a_list = list(map(int, input().split()))
    a = sum(a_list)
    b_list = list(map(int, input().split()))

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

    print(a - sum(a_list))


if __name__ == "__main__":
    main()

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

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

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

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

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

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

total = 0

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

Solution (Python):

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

        
count = 0

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

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

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

Solution (Python):

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

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

  if i != N:
    if B[i] > A[i]:
      t = A[i]
      A[i] = 0
      B[i] -= t
    else:
      t = B[i]
      B[i] = 0
      A[i] -= t
print(sumA - sum(A))

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

if __name__ == '__main__':
    main()

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

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):
def main():
    N = int(input())
    *A, = map(int, input().split())
    *B, = map(int, input().split())

    ans = 0
    for i, can_beat in enumerate(B):
        beat = min(A[i], can_beat)
        A[i] -= beat
        can_beat -= beat
        ans += beat

        beat = min(A[i + 1], can_beat)
        A[i + 1] -= beat
        ans += beat

    print(ans)


if __name__ == '__main__':
    main()

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

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

count = 0

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

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

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

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

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

curr = min(A[0], B[0])
ans = min(A[0], B[0])

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

Solution (Python):
N = int(input())
Monster = list(map(int, input().split()))
Yusha = list(map(int, input().split()))

total = 0

for i in range(N+1):
    if i > 0:
        Min = min(Monster[i], Yusha[i-1])
        Monster[i] -= Min
        Yusha[i-1] -= Min
        total += Min

    if i < N:
        Min = min(Monster[i], Yusha[i])
        Monster[i] -= Min
        Yusha[i] -= Min
        total += Min

print(total)

Solution (Python):

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n): return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)








n=n0()
a=n1()
b=n1()
ans=0

for i in range(n):
    a1,a2,b1=a[i],a[i+1],b[i]

    if a1-b1>=0:
        ans+=b1
    else:
        ans+=a1
        b1-=a1
        a[i+1]-=min(b1,a2)
        ans+=min(b1,a2)

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
total_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
        
    A[i+1] = max(0, A[i+1] - B[i])

total_after = sum(A)
print(total_before - total_after)

Solution (Python):



def main():
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

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

    print(ans)


if __name__ == '__main__':
    main()

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

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

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

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

p = 0
ans = 0
for enemy, power in zip(A, B):
    if enemy >= p:
        enemy -= p
        ans += p
    else:
        ans += enemy
        enemy = 0
        p = 0
    if enemy <= power:
        p = power - enemy
        ans += enemy
    else:
        ans += power
        p = 0
ans += min(p, A[-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):
  t = 0
  if b[i] <= a[i]:
    ans += b[i]
    a[i] -= b[i]
  else:
    t = a[i] + a[i+1]
    if b[i] > t:
      a[i+1] = 0
      ans += t
    else:
      a[i+1] -= b[i] - a[i]
      ans += b[i]
print(ans)

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

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

print(count)

Solution (Python):
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf


n = iinput()
a = iarr()
b = iarr()

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

Solution (Python):

import sys
input = sys.stdin.readline









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

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

print(ans)

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

assist = 0
kill = 0

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

if assist == 0:
    print(kill)
elif assist < a[-1]:
    print(kill + assist)
else:
    print(kill + a[-1])

Solution (Python):
'''
参考
https://note.com/tanon_cp/n/n6b51b9a57076
'''
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0
spl = 0                     
B.append(0)                 

for i in range(N+1):
    ans += min(spl, A[i])   
    mst = max(0, A[i]-spl)  

    ans += min(B[i], mst)
    spl = max(0, B[i]-mst)

print(ans)

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

Solution (Python):
import sys

sys.setrecursionlimit(10000000)
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue



ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


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


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()
    B = il()

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

    print(ret)


if __name__ == '__main__':
    main()

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

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

A.append(0)
A.append(0)

ai = A[0]
ai2 = A[1]

for i in range(N):

  bi = B[i]
  
  if ai <= bi:
    cnt += ai
    
    if ai2 <= (bi - ai):
      cnt += ai2
      ai = max(0, ai2-(bi - ai))
      ai2 = A[i+2]
    else:
      cnt += (bi - ai)
      ai = max(0, ai2-(bi - ai))
      ai2 = A[i+2]
      
  else:
    cnt += bi
    ai = A[i+1]
    ai2 = A[i+2]
  
print(int(cnt))

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

assist = 0
kill = 0

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

if assist == 0:
    print(kill)
elif assist < a[-1]:
    print(kill + assist)
else:
    print(kill + a[-1])

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

assist = 0
kill = 0

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

if assist == 0:
    print(kill)
elif assist < a[-1]:
    print(kill + assist)
else:
    print(kill + a[-1])

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

assist = 0
kill = 0

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

if assist == 0:
    print(kill)
elif assist < a[-1]:
    print(kill + assist)
else:
    print(kill + a[-1])

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

assist = 0
kill = 0

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

if assist == 0:
    print(kill)
elif assist < a[-1]:
    print(kill + assist)
else:
    print(kill + a[-1])

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

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

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

print(ans)

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

ans = 0
e = 0
for i, v in enumerate(b):
  if a[i] >= e:
      ans += e
      a[i] -= e
  else:
      ans += a[i]
      a[i] = 0
  e = 0
  if a[i] >= v:
      ans += v
  else:
      ans += a[i]
      e = v - a[i]
ans += min(a[-1], e)
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])
    nex=max(B[i]-A[i],0)
    ans+=min(nex, A[i+1])
    A[i+1]=max(A[i+1]-nex,0)

print(ans)

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