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

Solution (Python):
import numpy as np
N = int(input())
A = [int(i) for i in input().split()] 
B = [int(i) for i in input().split()] 
C = np.zeros(N+1)
count = 0
for i in range(N):
  a = A[i] - B[i] -C[i]
  if a >= 0:
    count += B[i] + C[i]
  else:
    count += A[i]
    if A[i] - C[i] < 0:
      C[i+1] = B[i]
    else:
      C[i+1] = -a
if C[N] > 0:
  if A[N] < C[N]:
    count += A[N]
  else:
    count += C[N]
print(int(count))

Solution (Python):
from copy import copy

N = int(input())
AAA = list(map(int, input().split()))
BBB = list(map(int, input().split()))

n_defeated = 0
for idx, (A, B) in enumerate(zip(AAA[:N + 1], BBB)):
    A = AAA[idx]
    Ap1 = AAA[idx + 1]
    mp_b = copy(B)
    
    
    if A > B:
        n_defeated += B
        AAA[idx] -= B
        mp_b = 0
    else:
        n_defeated += A
        AAA[idx] = 0
        mp_b = B - A
    
    
    if Ap1 > mp_b:
        n_defeated += mp_b
        AAA[idx + 1] -= mp_b
        mp_b = 0
    else:
        n_defeated += Ap1
        AAA[idx + 1] = 0
        mp_b -= Ap1
    

print(n_defeated)

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

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

Solution (Python):
N = int(input())
monster = [int(i) for i in input().split()]
brave_power = [int(i) for i in input().split()]
toubatsu_count = 0

for i in range(N):
    if brave_power[i]>monster[i]:
        toubatsu_count+=monster[i]
        nokori_brave = brave_power[i] - monster[i]
        
        if nokori_brave > monster[i+1]:
            toubatsu_count+=monster[i+1]
            monster[i+1]=0
        else:
            toubatsu_count+=nokori_brave
            monster[i+1]-=nokori_brave
    else:
        toubatsu_count+=brave_power[i]

print(toubatsu_count)

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


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

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


print(sum_A - sum(A))

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

print(d)

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

Solution (Python):
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    logging.info(A)
    logging.info(B)

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

    if B[N - 1] > A[N]:
        counter += A[N]
    elif B[N - 1] != 0:
        counter += B[N - 1]

    print(counter)
            
    
if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
N=int(input())
A=np.array(list(int(i) for i in input().split())  )
B=np.array(list(int(i) for i in input().split())  )

num=np.sum(A)

for i in range(len(B)):
    if A[i]<=B[i]:
        B[i]=B[i]-A[i]
        A[i]=0
    else:
        A[i]=A[i]-B[i]
        B[i]=0
    if A[i+1]>=B[i]:
        A[i+1]=A[i+1]-B[i]
    else:
        A[i+1]=0
        B[i]=B[i]-A[i+1]
print(num-np.sum(A))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
dp_1 = [0]*(n)
dp_1[0] = min(a[0], b[0])
dp_2 = [0]*(n)
dp_2[0] = min(a[1], b[0]-min(a[0], b[0]))
for i in range(n-1):
    dp_1[i+1] = min(b[i+1], a[i+1] - dp_2[i])
    dp_2[i+1] = min(a[i+2], b[i+1] - dp_1[i+1])
print(sum(dp_1+dp_2))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
B = [0] + B + [0]
total_ene = sum(A)


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

max_lose_ene = total_ene - survive_enemy

print(max_lose_ene)

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




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

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

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

A = [int(x) for x in input().split()]

B = [int(x) for x in input().split()]

i = 0
total = 0
C = [A[0]]
while i < N:
  C.append(A[i+1])
  if C[0] >= B[i]:
    total += (C[0] - B[i])
    del C[0]
  else:
    if sum(C) - B[i] >= 0:
      tmp = sum(C) - B[i]
    else:
      tmp = 0
    C.clear()
    C.append(tmp)
  i += 1 
    
print(sum(A) - (total+sum(C)))

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

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

Solution (Python):
input()
a=map(int,input().split())
b=map(int,input().split())
i=next(a)
r=0
for j,k in zip(a,b):
	s=min(i,k)
	k-=s
	t=max(min(j,k),0)
	j-=t
	i=j
	r+=s+t
print(r)

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

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

print(total - sum(a))

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
suslik = 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(suslik - sum(B))

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

temp = 0

for i, j in enumerate(A):
    if i == N:
        break
    if j >= B[i]:
        temp += B[i]
        B[i] = 0
        continue
    else:
        temp += j
        B[i] = B[i] - j

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

print(temp)

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

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

print(count)

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

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

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

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

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

print(m)

Solution (Python):
from collections import defaultdict


if __name__=="__main__":
    brave_n = int(input())
    monster_dic = defaultdict(int)
    brave_dic = defaultdict(int)
    monster_n = list(map(int, input().split()))
    brave_can = list(map(int, input().split()))
    kill_count = 0

    for i, m in enumerate(monster_n):
        monster_dic[i] = m

    for i, b in enumerate(brave_can):
        brave_dic[i] = b

    for i in range(len(brave_dic)):

        if brave_dic[i] >= monster_dic[i]:
            kill = monster_dic[i]
            monster_dic[i] -= kill
            brave_dic[i] -= kill
            kill_count += kill
            if brave_dic[i] >= monster_dic[i+1]:
                kill = monster_dic[i+1]
                monster_dic[i+1] -= kill
                brave_dic[i] -= kill
                kill_count += kill
            else:
                kill = brave_dic[i]
                monster_dic[i+1] -= kill
                brave_dic[i] -= kill
                kill_count += kill
        else :
            kill = brave_dic[i]
            monster_dic[i] -= kill
            brave_dic[i] -= kill
            kill_count += kill

    print(kill_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 += a[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):


import math

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    
    token = lines[0].split(" ")
    n = int(token[0])
    a = [int(i) for i in lines[1].split(" ")]
    b = [int(i) for i in lines[2].split(" ")]

    return (n, a, b)


def solve(n, a, b):
    
    result = 0
    for i in range(n):
        
        if a[i] >= b[i]:
            c = b[i]
            e = 0
            a[i] = a[i] - c
        else:
            c = a[i]
            d = b[i] - c
            a[i] = 0
            if a[i+1] >= d:
                e = d
                a[i+1] = a[i+1] - d
            else:
                e = a[i+1]
                a[i+1] = 0
        
        result = result + (c+e)

    return result


def main():
    

    print("%s" % solve(*parse_input()))


if __name__ == '__main__':

    main()

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

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

total += min(m, a[-1])
print(total)

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

for x in range(len(A)-1):
    count += min(A[x], B[x])
    nokori = B[x] - min(A[x], B[x])
    if nokori >= 0:
        count += min(A[x+1], nokori)
        A[x+1] -= min(A[x+1], nokori)
print(count)

Solution (Python):


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

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

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

    print(ans)

    return


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

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

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

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

Solution (Python):
import numpy as np

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
RA = [A[0]]

killed = []
for i in range(N):
    killed.append(min(RA[i], B[i]))
    RB = B[i] - killed[-1]
    killed[-1] += min(A[i+1], RB)
    RA.append(max(0, A[i+1]-RB))

print(sum(killed))

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

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

print(sum_hunt)

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

print(ans)

Solution (Python):
n = int(input())
enemies = list(map(int, input().split()))
heros = list(map(int, input().split()))
total = 0
remain = enemies[0]
for i in range(len(heros)):
    if heros[i] < remain:
        remain = enemies[i+1]
        total += heros[i]
    else:
        e = remain + enemies[i+1]
        total += min(heros[i], e)
        remain = e - heros[i] if e > heros[i] else 0
print(total)

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

sum = 0
rest_hero = 0

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

print(sum)

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

Solution (Python):

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

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

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

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

    else:
        c += (a[i]+a[i+1])
        a[i+1] = 0
print(c)

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

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

Solution (Python):
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
import numpy as np
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

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

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

monsters = list(map(int, input().split()))
powers = list(map(int, input().split()))


dp = [0 for _ in range(N + 1)]





dp[N] = min(monsters[N], powers[N - 1])

powers[N - 1] -= dp[N]

for i in range(N - 1, 0, -1):
    
    
    
    
    dp[i] = min(powers[i] + powers[i - 1], monsters[i])
    
    
    powers[i - 1] -= max(dp[i] - powers[i], 0)



dp[0] = min(powers[0], monsters[0])

print(sum(dp))

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

Solution (Python):
n = int(input())
arr = list(reversed(list(map(int,input().split()))))
brr = list(reversed(list(map(int,input().split()))))
brr.append(0)
cur=0
ans=0
for i in range(n+1):
    ans+=min(arr[i],cur)
    a=max(0,arr[i]-cur)
    ans+=min(a,brr[i])
    cur=max(brr[i]-a,0)
print(ans)

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

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

res = 0
amari = 0
amari0 = 0
amari1 = 0
for i in range(n):
    if i % 2 == 0:
        monster = max(0, a_array[i]-amari1)
        res += min(amari1, a_array[i])
        res += monster if b_array[i] >= monster else b_array[i]
        
        amari0 = 0 if b_array[i] <= monster else b_array[i]-monster
    else:
        monster = max(0, a_array[i]-amari0)
        res += min(amari0, a_array[i])
        res += monster if b_array[i] >= monster else b_array[i]
        
        amari1 = 0 if b_array[i] <= monster else b_array[i]-monster

if n % 2 == 0:
    monster = max(0, a_array[-1]-amari1)
    res += min(amari1, a_array[-1])
else:
    monster = max(0, a_array[-1]-amari0)
    res += min(amari0, a_array[-1])
print(res)

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

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

if suc >= a[n]:
    k += a[n]
else:
    k += suc
    
print(k)

Solution (Python):
N = int(input())
city = list(map(int, input().split()))
hero = list(map(int, input().split()))
ko = 0 

for i in range(0 ,N):
  if city [i] >= hero[i]:
    ko += hero[i]
  elif city [i] <= hero[i]:
    ko += city[i]
    extra = hero[i] - city[i]
    
    if extra >= city[i+1]:
      ko += city[i+1]
      city[i+1] = 0
    elif extra <= city[i+1]:
      ko += extra
      city[i+1] = city[i+1] - extra
print(ko)

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

    if A[i] <= B[i]:
        count += A[i]
        B[i] -= A[i]
        A[i] = 0
    else:
        count += B[i]
        A[i] -= B[i]
        B[i] = 0

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

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

total_mons = 0
debt = 0

if B[N-1] < A[N]:
    total_mons += B[N-1]
    B[N - 1] = 0
else:
    total_mons += A[N]
    B[N - 1] = B[N - 1] - A[N]


for i in reversed(range(1, N)):

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


if B[0] < A[0]:
    total_mons += B[0]
else:
    total_mons += A[0]

print(total_mons)

Solution (Python):
while True:
    try:
        sum=0
        n=eval(input())
        l1=list(map(int,input().split()))
        l2=list(map(int,input().split()))
        for i in range(n):
            ss=1
            if(l2[i]-l1[i]>0):
                s=l2[i]-l1[i]
                if l1[i+1]-s<0:
                    sum+=l1[i+1]
                    l1[i+1]=0
                else:
                    l1[i+1]-=s
                    sum+=s
                    sum+=l1[i]
                    ss=0
                if(ss!=0):sum+=l1[i]

            elif(l2[i]-l1[i]<=0):
                sum+=l2[i]
        print(sum)
    except:break

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

Solution (Python):
N = int(input())
mnstr = list(map(int, input().split()))
yusha = list(map(int, input().split()))
sum = 0
killed = 0

for i in range(N):
  killedtemp1 = min(mnstr[i], yusha[i])
  yusha[i] = max(0, yusha[i]-mnstr[i])
  killedtemp2 = min(mnstr[i+1], yusha[i])
  mnstr[i+1] = mnstr[i+1] - killedtemp2
  killed += killedtemp1 + killedtemp2
  
print(killed)

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

pre = sum(a)

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

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

    elif b[i] == a[i]:
        b[i] = a[i] = 0

print(pre-sum(a))

Solution (Python):
import sys
p = [[int(c) for c in r.split()] for r in sys.stdin.read().split('\n') if r != '']
n = p[0][0]
a = p[1]
b = p[2]
c = 0
k = 0
for i in range(n):
    v = min(a[i], k)
    w = min(b[i], a[i] - v)
    c += v + w
    k = b[i] - w
v = min(a[-1], k)
print(c+v)

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

counter = 0
for i in range(n):
  power = B[-i-1]
  counter += min(power,A[-i-1])
  power -= min(power,A[-i-1])
  counter += min(power,A[-i-2])
  A[-i-2] = max(0, A[-i-2]-power)
print(counter)

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

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

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

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

    M += A[i]
    B[i] -= A[i]
    A[i] = 0

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

    M += A[i+1]
    B[i] -= A[i+1]
    A[i+1] = 0

print M

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

count=0

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

print(count)

Solution (Python):
N=int(input())
a=input().split()
b=input().split()
for i in range(0,N):
  a[i]=int(a[i])
  b[i]=int(b[i])
a[N]=int(a[N])
answer=sum(a)
for i in range(0,N):
    c=b[i]-a[i]
    if c<0:
        a[i]=-c
        b[i]=0
    else:
        a[i]=0
        b[i]=c
    d=b[i]-a[i+1]
    if d<0:
        a[i+1]=-d
        b[i]=0
    else:
        a[i+1]=0
        b[i]=d
print (answer-sum(a))

Solution (Python):
import copy
N = int(input())
liA = list(map(int,input().split()))
liB = list(map(int,input().split()))
liC = copy.deepcopy(liA)
for i in range(len(liB)):
    if liA[i] >= liB[i]:
        liA[i] = liA[i]-liB[i]
    elif liB[i] - (liA[i]+liA[i+1]) <= 0:
        liA[i+1] = liC[i+1] + liA[i] - liB[i]
        liA[i] = 0
    else:
        liA[i] = 0
        liA[i+1] = 0
print(sum(liC)-sum(liA))