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

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

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

cnt = 0
for i in range(n):
    monster = min(a[i], b[i])
    a[i] -= monster
    b[i] -= monster
    cnt += monster

    monster = min(a[i+1], b[i])
    a[i+1] -= monster
    b[i] -= monster
    cnt += monster

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

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

SumA_old=sum(A)

P=[_ for _ in range(N)]

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

SumA_new=sum(A)

print(SumA_old-SumA_new)

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

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

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

total = 0
c = A[0]
for i in range(N):
	if c + A[i +1] <= B[i]:
		total += c + A[i +1]
		c = 0
	elif c >= B[i]:
		total += B[i]
		c = A[i +1]
	else:
		total += B[i]
		c = c + A[i +1] - B[i]
		
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(n):
    d = min(a[i], b[i])
    sum += d
    a[i] -= d
    b[i] -= d
    d = min(a[i+1], b[i])
    sum += d
    a[i+1] -= d
    b[i] -= d

print(sum)

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

Solution (Python):
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
c = a.copy()
b = list(map(int, input().split()))
for i in range(n):
    for j in range(2):
        if a[i+j] <= b[i]:
            b[i] -= a[i+j]
            a[i+j] = 0
        else:
            a[i+j] -= b[i]
            b[i] = 0
print(sum(c-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 += b[i]
  else:
    count += min(b[i], a[i]+a[i+1])
    a[i+1] = max(0, a[i+1]-(b[i]-a[i]))
print(count)

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

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

Solution (Python):
input()
monsters = [int(x) for x in input().split()]
hero = [int(x) for x in input().split()]
ans = 0
power_left = 0
for i, b in enumerate(hero[::-1],1):
    to_kill = min(monsters[-i], b+power_left)
    power_left = b - max(0, to_kill - power_left)
    ans += to_kill
ans += min(monsters[0], power_left)
print(ans)

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

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

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

Solution (Python):

def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]

    ans = 0
    for idx in range(N):
        for inc in range(2):
            if A[idx+inc] <= B[idx]:
                ans += A[idx+inc]
                B[idx] -= A[idx+inc]
                A[idx+inc] = 0
            else:
                ans += B[idx]
                A[idx+inc] -= B[idx]
                B[idx] -= B[idx]
    print(ans)


if __name__ == "__main__":
    main()

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

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

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

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
before=sum(A)
for i in range(len(B)):
    while B[i]>0:
        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]
            else:
                A[i+1]=0
            B[i]=0
after=sum(A)
print(before-after)

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

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

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 0
for i in range(N):
    b  = B[i]
    a  = A[i]
    a2 = A[i+1]

    if a <= b:
        ans += a
        A[i] = 0
        b -= a
    else:
        ans += b
        A[i] -= b
        b = 0

    if b > 0:
        if a2 <= b:
            ans += a2
            A[i+1] = 0
            b -= a2
        else:
            ans += b
            A[i+1] -= b
            b = 0
print(ans)

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

c = 0
for i in range(N):
    s = min(B[i],A[i])
    c += s
    B[i] = B[i]-s
    A[i] = A[i]-s
    t = min(B[i], A[i+1])
    c += t
    B[i] = B[i]-t
    A[i+1] = A[i+1]-t

print(c)

Solution (Python):
n = int(input())
monst = list(map(int,input().split()))
hero = list(map(int,input().split()))
attack = 0

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

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

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
AB = list(map(int,read().split()))

A = AB[:N+1]; B = AB[N+1:]

B.append(0)
rest = 0
answer = 0
for a,b in zip(A,B):
    x = rest if rest < a else a
    answer += x; a -= x
    x = a if a<b else b
    answer += x; rest = b-x

print(answer)

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

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

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

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

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

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

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

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

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

print(to)

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

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

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

Solution (Python):
def calcRestNum(a, b, ret):
    if a < b:
        ret += a
        b -= a
        a = 0
    else:
        ret += b
        a -= b
        b = 0
    return [a, b, ret]

if __name__ == '__main__':
    N = int(input())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]

    ret = 0
    for n in reversed(range(N)):
        A[n + 1], B[n], ret = calcRestNum(A[n + 1], B[n], ret)
        A[n], B[n], ret = calcRestNum(A[n], B[n], ret)
    print(ret)

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


print(ans)

Solution (Python):
N = int(input())
M = list(map(int, input().split()))
Y = list(map(int, input().split()))
mtotal = sum(M)


for idx in range(len(Y)):
  if Y[idx] >= M[idx]:
    Y[idx] -= M[idx]
    M[idx] = 0
    M[idx+1] = max(M[idx+1] - Y[idx], 0)
  else:
    M[idx] -= Y[idx]
    Y[idx] = 0
    
  
  

print(mtotal - sum(M))

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

first = sum(a)

if a[0] < b[0]:
  b[0] -= a[0]
  a[0] = 0
else:
  a[0] -= b[0]
  b[0] = 0
  
if a[n] < b[n-1]:
  b[n-1] -= a[n]
  a[n] = 0
else:
  a[n] -= b[n-1]
  b[n-1] = 0
  
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(first - sum(a))

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
def_Bsum = sum(B)
 
for i in range(N+1):
    if(i==0):                  
        if (A[i] < B[i]):
            B[i] -= A[i]
            A[i] = 0
        else:
            A[i] -= B[i]
            B[i] = 0
 
    if(0<i<N):
        if(A[i] < B[i-1]):
            B[i-1] -= A[i]
            A[i] = 0
        else:
            A[i] -= B[i-1]
            B[i-1] = 0
 
            if (A[i] < B[i]):
                B[i] -= A[i]
                A[i] = 0
            else:
                A[i] -= B[i]
                B[i] = 0
 
    if(i==N):
        if (A[i] < B[i-1]):
            B[i-1] -= A[i]
            A[i] = 0
        else:
            A[i] -= B[i-1]
            B[i-1] = 0
 
 
ans = def_Bsum - sum(B)

print(ans)

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




def_Bsum = sum(B)

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

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

            if (A[i] < B[i]):
                B[i] -= A[i]
                A[i] = 0
            else:
                A[i] -= B[i]
                B[i] = 0

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


ans = def_Bsum - sum(B)



print(ans)

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

ans1[0]=min(a[0],b[0])
ans2[0]=min(a[1],b[0]-min(a[0],b[0]))

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

print(sum(ans1)+sum(ans2))

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

o=0

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

print(o)

Solution (Python):
n = int(input())
li_a = list(map(int, input().split()))
li_b = list(map(int, input().split()))

li_a_sum = sum(li_a)
answer = 0


for idx in range(n):
    if li_a[idx] >= li_b[idx]:
        li_a[idx] -= li_b[idx]
        li_b[idx] = 0
    else:
        li_b[idx] = li_b[idx] - li_a[idx]
        li_a[idx] = 0

    if li_b[idx] > 0:
        if li_a[idx + 1] >= li_b[idx]:
            li_a[idx + 1] = li_a[idx + 1] - li_b[idx]
            li_b[idx] = 0
        else:
            li_b[idx] -= li_a[idx + 1]
            li_a[idx + 1] = 0

print(li_a_sum - sum(li_a))

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

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

c=0
for i in range(n):
      ci=min(a[i],b[i])
      c += ci
      b[i] -= ci
      ci1 = min(a[i+1],b[i])
      c += ci1
      a[i+1] -= ci1

print(c)

Solution (Python):
from collections import deque

def main():
    n = int(input())
    a = list(int(x) for x in input().split())
    b = list(int(x) for x in input().split())
    
    que = deque(a)
    ans = 0
    
    for i in b:
        q1 = que.popleft()
        if i >= q1:
            ans += q1
            q2 = que.popleft()
            if i-q1 >= q2:
                ans += q2
                que.appendleft(0)
            else: 
                ans += i-q1
                que.appendleft(q2-(i-q1))
        else: 
            ans += i
    print(ans)

if __name__ == '__main__':
    main()

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

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

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

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

print(max-sum(b))

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

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

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

print(total-a[-1])

Solution (Python):
def City():
  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(len(B)):
    if A[i]<=B[i]:
      ans += A[i]
      B[i] -= A[i]
      A[i] = 0
    else:
      ans += B[i]
      A[i] -= B[i]
      B[i] = 0
    if A[i+1]<=B[i]:
      ans += A[i+1]
      B[i] -= A[i+1]
      A[i+1] = 0
    else:
      ans += B[i]
      A[i+1] -= B[i]
      B[i] = 0
  print(ans)
if __name__=="__main__":
  City()

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

C = A.copy()
for i in range(n):
    if A[i] >= B[i]:
        A[i] -= B[i]
    else:
        tmp = B[i] - A[i]
        A[i] = 0
        A[i + 1] -= tmp if A[i + 1] >= tmp else A[i + 1]

print(sum([c - a for (c, a) in zip(C, A)]))

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    b = list(map(int, input().rstrip('\n').split()))
    cnt = 0
    for i in range(n):
        h = min(a[i], b[i])
        cnt += h
        a[i] -= h
        b[i] -= h
        h = min(a[i+1], b[i])
        cnt += h
        a[i+1] -= h
        b[i] -= h
    print(cnt)


if __name__ == '__main__':
    slove()

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

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

Solution (Python):
from fractions import gcd
from math import ceil,floor,sqrt
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
    b[i] -= tmp
    tmp = min(a[i+1], b[i])
    ans += tmp
    a[i+1] -= tmp

print(ans)

Solution (Python):
def func(N, A_i, B_i):
    ans = 0
    for i in range(N):
        ans += min(A_i[i], B_i[i]) + min(A_i[i+1], max(0, B_i[i] - A_i[i]))
        A_i[i+1] = max(0, A_i[i+1] - max(0, B_i[i] - A_i[i]))
    return ans

N = int(input())
A_i = list(map(int, input().split(" ")))
B_i = list(map(int, input().split(" ")))
print(func(N, A_i, B_i))

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


if __name__ == '__main__':
    main()

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

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

Solution (Python):
N = int(input())
A_l = list(map(int, input().split()))
B_l = list(map(int, input().split()))



killed_monsters = 0

for idx in range(N-1):
    killed_monsters += min(A_l[idx], B_l[idx])
    if B_l[idx] - A_l[idx] > 0:
        killed_monsters += min(B_l[idx] - A_l[idx], A_l[idx+1])
        A_l[idx+1] = max(0, A_l[idx+1] - (B_l[idx] - A_l[idx]))

killed_monsters += min(A_l[-2] + A_l[-1], B_l[-1])

print(killed_monsters)

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

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

print(answer)

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

ans=0
z=0

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

print(ans+min(z,A[N]))

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

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

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

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

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

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

print(knock)

Solution (Python):
from sys import stdin


def main():
    lines = stdin.readlines()
    A = tuple([int(i) for i in lines[1].split()])
    B = tuple([int(i) for i in lines[2].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(int(i) for i in lines[1].split())
    B = tuple(int(i) for i in lines[2].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 = [int(i) for i in lines[1].split()]
    B = [int(i) for i in lines[2].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 = 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()