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

count = 0
next_b = 0
for i, ai in enumerate(a):
  
  if i < n:
    bi = b[i]
    if ai > next_b:
      ai = ai - next_b
      count += next_b
    else:
      count += ai
      ai = 0
      
    if ai < bi:
      count += ai
      next_b = bi -ai
    else:
      count += bi
      next_b = 0
  else:
    if ai > next_b:
      count += next_b
    else:
      count += ai
      
print(count)

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

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

print(ans)

Solution (Python):


def main():
    n = int(input())
    A = list(map(int, input().rstrip().split()))
    B = tuple(map(int, input().rstrip().split()))
    ans = 0
    for i in range(n):
        cur1 = min(A[i], B[i])
        cur2 = min(A[i + 1], B[i] - cur1)
        A[i] -= cur1
        A[i + 1] -= cur2
        ans += cur1 + cur2
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

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

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
b.append(0)
sum1=0
rest=0
for i in range(N+1):
  x=min(a[i],b[i]+rest)
  sum1+=x
  a[i]-=x
  b[i]=min(b[i],b[i]+rest-x)
  if a[i]==0:
    rest=b[i]
  if b[i]==0:
    rest=0

print(sum1)

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

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


print(cnt)

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


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


print(cnt)

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

monster = 0
addPower = 0

for i in range(N):
    
    AA = A[i] + A[i+1]
    BA = B[i] - A[i]
    BAA= B[i] - AA
    if BAA >= 0:
        
        monster += AA
        A[i+1] -= min(A[i+1], BA)
    elif BA > 0:
        
        monster += B[i]
        A[i+1] -= min(A[i+1], BA)
    else:
        
        monster += B[i]
print(monster)

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


nmon = 0
c = 0
for i in range(n):
	
	if c > a[i]:
		nmon += a[i]
		c = b[i]
		continue
	elif b[i] + c > a[i]:
		nmon += a[i]
		c = b[i] + c - a[i]
	else:
		nmon += b[i]+c
		c = 0
if a[n] >= c:
	nmon += c
else:
	nmon += a[n]
print(nmon)

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

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

Solution (Python):
n = int(input())
monsters = list(map(int, input().split()))
heros = list(map(int, input().split()))

killed = 0
attack = 0
attack_l = [0, heros[0]]
for i in range(0, n+1):
  if i != n and i != 0:
    attack_l[1] = heros[i]
  attack = attack_l[0] + attack_l[1]
  if (monsters[i] - attack) < 0:
    killed += monsters[i]
    if (attack_l[0] - monsters[i]) < 0:
      attack_l[1] += (attack_l[0] - monsters[i])
    attack_l[0] = attack_l[1]
    attack_l[1] = 0
  elif (monsters[i] - attack) > 0:
    killed += attack
    attack_l[0] = 0
    attack_l[1] = 0
  elif (monsters[i] - attack) == 0:
    killed += monsters[i]
    attack_l[0] = 0
    attack_l[1] = 0

print(killed)

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

print(ans)

Solution (Python):
import numpy as np

N = int(input()) + 1

A = input().split()

B = input().split()

enemy = np.zeros(N)
saver = np.zeros(N)

beated = 0
over = 0

for i in range(N - 1):
    enemy[i] = int(A[N - i - 1])
    saver[i] = int(B[N - i - 2])

enemy[N - 1] = int(A[0])

for i in range(N):
    if(enemy[i] - over > saver[i]):
        beated = beated + saver[i] + over
        over = 0
    else:
        beated = beated + enemy[i]
        if(over > enemy[i]):
            over = saver[i]
        else:
            over = saver[i] + over - enemy[i]

print(int(beated))

Solution (Python):
N = int(input())
teki = input().split()
yusya = input().split()
i = 0
taoshita = 0

while i < N :
  if int(teki[i]) >= int(yusya[i]):
    a = int(yusya[i]) 
    b = 0
  else:
    a = int(teki[i])
    yusya[i] = int(yusya[i]) - int(teki[i])
    if int(teki[i+1]) >= yusya[i]:
      b = yusya[i]
      teki[i+1] = int(teki[i+1]) - yusya[i]
    else:
      b = int(teki[i+1])
      teki[i+1] = 0
  taoshita += a + b
  i += 1
  
print(taoshita)

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

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

    if a[-2-i] - b[-1-i] >= 0:
        a[-2-i] = a[-2-i] - b[-1-i]
    else:
        a[-2-i] = 0

print(sum_a - sum(a))

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

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

mons = [int(i) for i in input().split()]
yuusha = [int(i) for i in input().split()]

taoshita = 0

for i in range(len(yuusha)):
    if mons[i] > yuusha[i]:
        taoshita = taoshita + yuusha[i]
        mons[i] = mons[i] - yuusha[i]
        yuusha[i] = 0
    else:

        if mons[i] + mons[i+1] < yuusha[i]:
            taoshita = taoshita + mons[i] + mons[i+1]
            mons[i+1] = 0

        else:
            if mons[i] > yuusha[i]:
                taoshita = taoshita + yuusha[i]
            else:
                taoshita = taoshita + yuusha[i]
                mons[i+1] = mons[i+1] - (yuusha[i] - mons[i])


print(taoshita)

Solution (Python):
inf = 10 ** 18
P = 10 ** 9 + 7

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

A_all = sum(A)

xi = min(A[0], B[0])
A[0] -= xi
B[0] -= xi
for i in range(N):
    yi = min(B[i], A[i + 1])
    A[1 + i] -= yi
    B[i] -= yi
    
    if i < N - 1:
        xi = min(A[1 + i], B[1 + i])
        A[1 + i] -= xi
        B[1 + i] -= xi
    
    
    
A_r = list(reversed(A))
B_r = list(reversed(B))


xi = min(A_r[0], B_r[0])
A_r[0] -= xi
B_r[0] -= xi
for i in range(N):
    yi = min(B_r[i], A_r[i + 1])
    A_r[1 + i] -= yi
    B_r[i] -= yi
    
    if i < N - 1:
        xi = min(A_r[1 + i], B_r[1 + i])
        A_r[1 + i] -= xi
        B_r[1 + i] -= xi
    
    

print(A_all - sum(A))

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

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

i = N
if A[i] >= B[i]:
    A[i] -= B[i]
    B[i] = 0
else:
    B[i] -= A[i]
    A[i] = 0

sum_A -= sum(A)
print(sum_A)

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

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

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

dp_town = [0]*(n+1)
dp_town[0] = min(a[0],b[0])
b[0] = b[0] - min(a[0],b[0])
for i in range(1,n):
    if b[i-1] > 0:
        dp_town[i] = min(a[i],b[i-1])
        a[i] -= b[i-1]
        b[i-1] -= min(a[i],b[i-1])
    if a[i] > 0:
        dp_town[i] += min(a[i],b[i])
        b[i] -= min(a[i],b[i])
    if i == n-1 and b[i] > 0:
        dp_town[n] += min(a[n],b[i])

print(sum(dp_town))

Solution (Python):



N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

count = 0

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

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

print(count)

Solution (Python):
"""


 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())


 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  


a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654


char_list = list("abcd") 
"""

import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    n= int(sys.stdin.readline())
    a= list(map(int, sys.stdin.readline().split()))
    b= list(map(int, sys.stdin.readline().split()))

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

i = N - 1
k = 0

while i >= 0:

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

    i -= 1

print(k)

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

Solution (Python):
n = int(input())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
count = 0
for i in range(n):
  if a_list[i]+a_list[i+1] <= b_list[i]:
    count += a_list[i]+a_list[i+1]
    a_list[i+1] = 0
  else:
    count += b_list[i]
    if a_list[i] < b_list[i]:
      a_list[i+1] = a_list[i]+a_list[i+1] - b_list[i]
    

print(count)

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

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



print(str(ans))

Solution (Python):





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

Solution (Python):

import sys
input = sys.stdin.readline

n = int(input())

monster = list(map(int,input().split()))
yusha = list(map(int,input().split()))
kill = 0
overkill = 0
for i in range(len(yusha)):
    
    if monster[i] >= yusha[i]:
        kill += yusha[i]
    else:
        kill += monster[i]
        overkill = yusha[i] - monster[i]
        if monster[i+1] >= overkill:
            kill += overkill
            monster[i+1] -= overkill
        else:
            kill += monster[i+1]
            monster[i+1] = 0
    
            
print(kill)

Solution (Python):


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

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

if __name__ == '__main__':
    print(main())

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

index = 0
answer = 0

for i in B:
    next = i - A[index]
    if next >= 1:
        answer += A[index]
        if A[index+1] >= next:
            answer += next
            A[index+1] -= next
        else:
            answer += A[index+1]
            A[index+1] = 0
    else:
        answer += i
    index += 1

print(answer)

Solution (Python):
def main():
    n = int(input())
    a_s = list(map(int, input().split(' ')))
    b_s = list(map(int, input().split(' ')))
    count = 0
    carry_over = 0
    for i, a in enumerate(a_s):
        count += min(carry_over, a)
        a = max(a-carry_over, 0)
        b = b_s[i] if i < n else 0
        count += min(a, b)
        carry_over = max(0, b-a)
    print(count)


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
flg=0
Ttmp=0
for i in range(N):
    if flg == 1:
        Atmp=Ttmp
    else:
        Atmp=A[i]
    flg=0
    Btmp=B[i]

    if Atmp <= Btmp:
        ans+=Atmp
        Btmp-=Atmp
    else:
        ans+=Btmp
        continue
    flg=1        
    if A[i+1] <= Btmp:
        ans+=A[i+1]
        Ttmp=0
    else:
        ans+=Btmp
        Ttmp=A[i+1]-Btmp

print(ans)

Solution (Python):
n=int(input())
A=map(int,input().split())
B=map(int,input().split())
a=list(A)
b=list(B)
s=0
m=0

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

if a[n]<m:
    s=s+a[n]
else:
    s=s+m


print(s)

Solution (Python):

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

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

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

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

Solution (Python):
from sys import stdin

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


c=a

max=0
"""
for i in range(n+1):
    max+=a[i]"""

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


print(max)

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

beaten_monster_num = 0

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

print(int(beaten_monster_num))

Solution (Python):
N = int(input())
Alist = [int(i) for i in list(input().split())]
Blist = [int(i) for i in list(input().split())]
ans = 0
for i in range(N):
    tmp = Alist[i]-Blist[i]
    if tmp < 0:
        ans += Alist[i]
        Blist[i] = -1 * (tmp)
    else:
        ans += Blist[i]
        continue
    tmp = Alist[i+1] - Blist[i]
    if tmp < 0:
        ans += Alist[i+1]
        Alist[i + 1] = 0
    else:
        ans += Blist[i]
        Alist[i+1] = tmp

print(ans)

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

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

    if A[i+1] >= B[i]:
        total += B[i]
        A[i+1] = A[i+1] - B[i]
    elif A[i+1] < B[i]:
        total += A[i+1]
        A[i+1] = 0

print(total)

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

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

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

killed = 0
reserved_power = 0

if N == 1:
    battle = B[0] - A[0]
    if battle >= 0:
        killed += A[0]
        reserved_power = battle
        killed += min(reserved_power, A[1])
    else:
        killed += B[0]
    print(killed)
    exit()

for i in range(N):

    if i == 0:
        battle = B[i] - A[i]
        if battle >= 0:
            reserved_power = battle
            killed += A[i]
        else:
            reserved_power = 0
            killed += B[i]

    elif i == N - 1:
        other_monster = A[i] - reserved_power
        if other_monster <= 0:
            reserved_power = B[i]
            killed += A[i]
            killed += min(reserved_power, A[i+1])
        else:
            killed += reserved_power
            battle = B[i] - other_monster - A[i+1]
            if battle >= 0:
                killed += other_monster + A[i+1]
            else:
                killed += B[i]

    else:
        other_monster = A[i] - reserved_power

        if other_monster <= 0:
            reserved_power = B[i]
            killed += A[i]
        else:
            killed += reserved_power
            battle = B[i] - other_monster

            if battle >= 0:
                reserved_power = battle
                killed += other_monster
            else:
                reserved_power = 0
                killed += B[i]

print(killed)

Solution (Python):
input()

a=list(map(int,input().split(" ")))
b=list(map(int,input().split(" ")))

lenB=len(b)
count=0
for i in range(lenB):
    if b[i]>a[i]:
        count+=a[i]
        b[i]-=a[i]
        a[i]=0
        if b[i]>a[i+1]:
            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
    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()))
A.insert(0, 0) 
B.insert(0, 0) 
C=[0]*(N+1)     
D=[0]*(N+1) 
E=[0]*(N+2) 
F=[0]*(N+2) 
 
for i in range(1,N+1):
  if i==1:
    F[i]=B[i]
  else:
    F[i]=B[i]+C[i-1] 
    
  if F[i]>=A[i]: 
    E[i]=A[i]
    C[i]=min(F[i]-A[i],B[i])
  else:
    E[i]=F[i]
    C[i]=0
 
  
 
if C[N]>=A[N+1]: 
  E[N+1]=A[N+1]
else:
  E[N+1]=C[N]
 
ans=sum(E)

print(ans)

Solution (Python):
import copy

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

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

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

Solution (Python):

a = int(input())
mon = list(map(int, input().split(" ")))
att = list(map(int, input().split(" ")))
kil = 0

b = len(mon)

for i in range(b):

    if i == a :
        if  mon[i] >= att[i-1]:
            kil = kil + att[i-1]
        elif mon[i] < att[i-1] :
            kil = kil + mon[i]
        break
    elif i > 0 and att[i-1] > 0 :
        if mon[i] >= att[i-1]:
            kil = kil + att[i-1]
            mon[i] = mon[i] - att[i-1] 
        elif mon[i] < att[i-1] :
            kil = kil + mon[i]
            continue
    if mon[i] >= att[i]:
        kil = kil + att[i]
        att[i] = 0
    elif mon[i] < att[i] :
        kil = kil + mon[i]
        att[i] = att[i] - mon[i]



        
print(kil)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy,copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    n=INT()
    a=LIST()
    b=LIST()
    p=sum(a)

    for i in range(n):
        tmp=max(b[i]-a[i], 0)
        a[i]=max(a[i]-b[i],0)

        a[i+1] = max(a[i+1]-tmp,0)
        

    
    print(p-sum(a))

if  __name__=='__main__':
    main()

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

Solution (Python):
import sys



def solve(lines):
    N = int(lines.pop(0))
    A = list(map(int, lines.pop(0).split(' ')))
    B = list(map(int, lines.pop(0).split(' ')))

    res = 0
    ex  = 0

    for i,x in enumerate(A):
        if ( i <= len(B) - 1 ):
            bi = B[i]
        else:
            bi = 0
        if ( ex > 0 ):
            if ( ex - x >= 0 ):
                res += x
                ex = bi
                continue
            else:
                res += ex
                x -= ex
        if (bi - x >= 0):
            res += x
            bi -= x
            ex = bi
        else:
            res += bi
            ex = 0

    print(res)


lines = [x.strip() for x in sys.stdin.readlines()]




solve(lines)

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

t = b[0]
t2 = b[-1]
count = 0

half = n // 2

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

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

if n % 2 == 1:
  t = a[half] + a[half+1]
  if t > b[half]:
    count += b[half]
  else:
    count += t

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

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

taos = 0
keep = 0

for i, (a, b) in enumerate(zip(A_list, B_list)):
  if a >= (b + keep) :
    taos += (b + keep)
    keep = 0
  else:
    taos += a
    if keep > a:
      keep = b
    else:
      keep = (b + keep) - a 
  

if A_list[-1] >= keep:
  taos += keep
else:
  taos += A_list[-1]
print(taos)

Solution (Python):
heroes = int(input())
puts = input()
monsters = puts.split()
puts = input()
strength = puts.split()
Ans = 0

for i in range(heroes):
	if (int(strength[i]) > int(monsters[i])):
		if(int(strength[i]) > int (monsters[i]) + int (monsters[i+1])):
			Ans += int(monsters[i]) + int(monsters[i+1])
			monsters[i+1] = 0
		else:
			monsters[i+1] = int(monsters[i+1]) + int(monsters[i]) - int(strength[i])
			Ans += int(strength[i])
	else:
		Ans += int(strength[i])

print(Ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
sys.setrecursionlimit(10**7)
inf = 10**20

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

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

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

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

temp=0

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

print(sum(c))

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

print(d)