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(B)):
    
    if B[i]>A[i]:
        mod=min(A[i+1],B[i]-A[i])
        A[i+1]-=mod
        
        ans+=int(A[i]+mod)
   
    
    else:
        ans+=B[i]

print(ans)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = 0
j = 0
f = []
while (j < len(a)):
    f.append(a[j])
    j += 1

while (i < n):
    if (a[i]-b[i] < 0):
        b[i] -= a[i]
        a[i] = 0
        if (a[i+1] < b[i]):
            a[i+1] = 0
        else:
            a[i+1] -= b[i]
    else:
        a[i] -= b[i]
    i += 1
print (sum(f) - sum(a))

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

counter = 0
for i in range(len(b)):
    counter += min(a[i],b[i])
    if b[i] - a[i] >= 0:
        c = b[i]-a[i]
        if a[i + 1] - c >= 0:
            a[i + 1] -= c
            counter += c
        else:
            counter += a[i + 1]
            a[i + 1] = 0

print(counter)

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

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

if __name__ == '__main__':
	main()

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

count = 0
for i in range(n)[::-1]:
    if a[i+1] > 0:
        score = min(a[i+1], b[i])
        count += score
        a[i+1] -= score
        b[i] -= score
    if b[i] > 0:
        score = min(a[i], b[i])
        count += score
        a[i] -= score
        b[i] -= score
print(count)

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

sum=0

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Ans = sum(B)
AT = []
for i in range(N//2+1):
  AT+=[[i,i],[i+1,i]]
  AT+=[[N-i,N-i-1],[N-i-1,N-i-1]]
AT = AT[:2*N]
for i in AT:
  X = min(A[i[0]],B[i[1]])
  A[i[0]]=A[i[0]]-X
  B[i[1]]=B[i[1]]-X
Ans = Ans - sum(B)
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):
  m=min(a[i],b[i])
  b[i]-=m
  r=min(a[i+1],b[i])
  a[i+1]-=r
  count+=m+r
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(len(B)):
    temp = min(A[i], B[i])
    A[i] -= temp
    B[i] -= temp
    total += temp
    temp = min(A[i+1], B[i])
    A[i+1] -= temp
    B[i] -= temp
    total += temp
print(total)

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

print(ans)

Solution (Python):
from itertools import *
from math import *

INF = int(1e18)
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
before = sum(A)

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

print(before - after)

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
before = sum(A)

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

print(before - after)

Solution (Python):
def city_savers(N: int, A: list, B: list)->int:
    ans = 0
    for i in range(N):
        attacked = min(B[i], A[i])
        B[i] -= attacked
        A[i] -= attacked
        ans += attacked

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

    
    
    return ans


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = city_savers(N, A, B)
    print(ans)

Solution (Python):
def calculate(arr1,arr2):

    result = 0
    for i in range(len(arr2)):

        if (arr1[i] + arr1[i+1]) >= arr2[i]:
            result += arr2[i]
        else:
            result += arr1[i] + arr1[i+1]

        res1 = arr2[i] - arr1[i]

        if arr2[i] >= arr1[i]:
            arr1[i] = 0
        else:
            arr1[i] = arr1[i] - arr2[i]

        if res1 > 0:
            if arr1[i+1] >= res1:
                arr1[i+1] = arr1[i+1] - res1
            else:
                arr1[i+1] = 0


    return result


N = int(input())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))

result = calculate(arr1, arr2)

print(result)

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
x = 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
y = sum(b)
print(x-y)

Solution (Python):
import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N = INT()
A = MAP()
B = MAP()

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

print(ans)

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

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

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

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

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

Solution (Python):
def main():
    n = int(input())
    monsters = [int(x) for x in input().split()]
    heroes = [int(x) for x in input().split()]

    sum_monsters = sum(monsters)
    for i in range(n-1, -1, -1):
        if monsters[i+1] >= heroes[i]:
            monsters[i+1] -= heroes[i]
            heroes[i] = 0
        else:
            temp_m = monsters[i+1]
            monsters[i+1] = 0
            heroes[i] -= temp_m
            if monsters[i] >= heroes[i]:
                monsters[i] -= heroes[i]
                heroes[i] = 0
            else:
                temp_m = monsters[i]
                monsters[i] = 0
                heroes[i] -= temp_m

    ans = sum_monsters - sum(monsters)
    print(ans)


if __name__ == '__main__':
    main()

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

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

    if ex != 0:
        if a[i+1] - ex <= 0:
            knock += a[i+1]
            a[i+1] = 0
        else:
            knock += ex
            a[i+1] -= ex

print(knock)

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

ret = 0 

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

        if (A[i+1] + k) > 0:
            A[i+1] += k
            ret += abs(k)
        else:
            ret += A[i+1]
            A[i+1] = 0
    else:
        ret += B[i]
        
print(ret)

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

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

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

point = 0
i = 0
while (i < N):
  if A[i] >= B[i]:
    point += B[i]
  else:
    if(A[i] + A[i+1]) > B[i]:
      point += B[i]
      A[i+1] -= (B[i] - A[i])
    else:
      point += (A[i] + A[i+1])
      A[i+1] = 0
  i += 1
print(point)

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

m = sum(a)

for i in range(n):
    k = min(a[i], b[i])
    a[i] -= k
    b[i] -= k
    a[i + 1] -= min(a[i + 1], b[i])

print(m - sum(a))

Solution (Python):

import math
import heapq
import bisect

MOD = 10**9+7

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(0,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()))
ans = 0
for i in range(N):
  if A[i] > B[i]:
    ans += B[i]
  elif A[i] <= B[i] < A[i]+A[i+1]:
    ans += B[i]
    A[i+1] -= B[i] - A[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 = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
ans = 0

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

if b[n-1] != 0:
    if a[n] > b[n-1]:
        ans += b[n-1]
    else:
        ans += a[n]

print(ans)

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

ans = sum(A)

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

print(ans - sum(A))

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

Solution (Python):
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    *A, = map(int, readline().split())
    *B, = map(int, readline().split())
    ans = 0
    pre = 0
    for a, b in zip(A, B):
        brave = pre + b
        if brave < a:
            ans += brave
            pre = 0
        elif a < pre:
            ans += a
            pre = b
        else:
            ans += a
            pre = brave - a
    if A[-1] < pre:
        ans += A[-1]
    else:
        ans += pre
    print(ans)
    return


main()

Solution (Python):
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    *A, = map(int, readline().split())
    *B, = map(int, readline().split())
    B.append(0)
    ans = 0
    pre = 0
    for a, b in zip(A, B):
        brave = pre + b
        if brave < a:
            ans += brave
            pre = 0
        elif a < pre:
            ans += a
            pre = b
        else:
            ans += a
            pre = brave - a
    print(ans)
    return


main()

Solution (Python):
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    *A, = map(int, readline().split())
    *B, = map(int, readline().split())
    B.append(0)
    ans = 0
    pre = 0
    for a, b in zip(A, B):
        brave = pre + b
        if brave < a:
            ans += brave
            pre = 0
        elif a < pre:
            ans += a
            pre = b
        else:
            ans += a
            pre = brave - a
    if A[-1] < pre:
        ans += A[-1]
    else:
        ans += pre
    print(ans)
    return


main()

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

Solution (Python):
from sys import stdin

input = stdin.readline


def main():
    input()
    *A, = map(int, input().split())
    *B, = map(int, input().split())
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
from sys import stdin

input = stdin.readline


def read():
    return map(int, input().split())


def main():
    input()
    *A, = read()
    *B, = read()
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
from sys import stdin


def main():
    stdin.readline()
    *A, = map(int, stdin.readline().split())
    *B, = map(int, stdin.readline().split())
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    *A, = map(int, readline().split())
    *B, = map(int, readline().split())
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
from sys import stdin

readline = stdin.readline


def read():
    return map(int, readline().split())


def main():
    input()
    *A, = read()
    *B, = read()
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
from sys import stdin

input = stdin.readline


def read():
    return tuple(map(int, input().split()))


def main():
    input()
    A = read()
    B = read()
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
def read():
    return tuple(map(int, input().split()))


def main():
    input()
    A = read()
    B = read()
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
from sys import stdin

readline = stdin.readline


def read():
    return tuple(map(int, readline().split()))


def main():
    readline()
    A = read()
    B = read()
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    A = tuple(map(int, readline().split()))
    B = tuple(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
from sys import stdin


def main():
    input()
    readline = stdin.readline
    A = tuple(map(int, readline().split()))
    B = tuple(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
from sys import stdin

readline = stdin.readline


def main():
    input()
    A = tuple(map(int, readline().split()))
    B = tuple(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
from sys import stdin

readline = stdin.readline


def main():
    readline()
    A = tuple(map(int, readline().split()))
    B = tuple(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
from sys import stdin


def main():
    input()
    A = tuple(map(int, stdin.readline().split()))
    B = tuple(map(int, stdin.readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Solution (Python):
from sys import stdin


def main():
    lines = stdin.readlines()
    A = tuple(map(int, lines[1].split()))
    B = tuple(map(int, lines[2].split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = min(pre, b)
        b -= tmp1
        tmp2 = min(a, b)
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

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

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

sum_a = 0
rest_a = a_list[0]
for i in range(n):
  if b_list[i] <= rest_a:
    sum_a += b_list[i]
    rest_a = a_list[i+1]
  elif b_list[i] >= rest_a + a_list[i+1]:
    sum_a += rest_a + a_list[i+1]
    rest_a = 0
  else:
    sum_a += b_list[i]
    rest_a = a_list[i+1] - b_list[i] + rest_a

print(sum_a)

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

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

print(ans)

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

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

    if i == n:
        break
    elif a[i] <= b[i]:
        c = b[i] - a[i]
        ans += a[i]
        
    else:
        c = 0
        ans += b[i]
        
    

print(ans)

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

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

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

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

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

ans = 0

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


print(ans)

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

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

Solution (Python):
N = int(input())
mon = [int(x) for x in input().split()]
her = [int(x) for x in input().split()]
n = 0
for i in range(len(her)):
    z = min(mon[i], her[i])
    mon[i] -= z
    her[i] -= z
    n += z
    z = min(mon[i+1], her[i])
    mon[i+1] -= z
    her[i] -= z
    n += z
print (n)

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

  
print(count)

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

a = [num for num in map(int, input().split(' '))]
b = [num for num in map(int, input().split(' '))]

ans = 0
pre = 0 

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


print(ans)

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

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

print(out)

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(lambda x: int(x), input().split()))
B = list(map(lambda x: int(x), input().split()))

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

print(sum)

Solution (Python):
N = int(input())
Alist = list(map(int, input().split()))
Blist = list(map(int, input().split()))
help = 0
kill = 0
for i in range(N):
    monster = Alist[i] - help
    if monster >= 0:
        kill += help
    else:
        kill += Alist[i]
        monster = 0

    can_kill = Blist[i]
    if can_kill >= monster:
        kill += monster
        help = can_kill - monster
    else:
        kill += can_kill
        help = 0
monster = Alist[-1]
if help >= monster:
    kill += monster
else:
    kill += help
print(kill)

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

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

ans = 0
for idx, item in enumerate(B):
    a = min(item, A[idx])
    ans += a
    if (item > a):
        if ((item-a) < A[idx+1]):
            ans += (item-a)
            A[idx+1] -= (item-a)
        else :
            ans += A[idx+1]
            A[idx+1] = 0     

print(ans)