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

t = 0

for i in range(n):
    if i != n-1:
    

        
        if b[i] < a[i]:
            t += b[i]

        
        else:
            
            if a[i+1] > b[i] - a[i]:
                t += min(a[i+1]+a[i],b[i])
                a[i+1] = max(0,a[i+1] + a[i]- b[i])

            
            else:
                t += a[i]+a[i+1]
                a[i+1] = 0
            
            
        
    else:
        if b[i] - a[i] < 0:
            t += b[i]
        else:
            t += (a[i] + min(b[i] - a[i],a[i+1]))

    

        
print(t)

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

kill = 0 
for i in reversed(range(1, len(A))):
  if A[i] > 0:
    d = min(A[i], B[i-1])
    B[i-1] -= d
    A[i] -= d
    kill += d

  if (A[i-1] > 0):
    d = min(A[i-1], B[i-1])
    B[i-1] -= d
    A[i-1] -= d
    kill += d

print(kill)

Solution (Python):
import sys
import numpy as np
from scipy.misc import comb
sys.setrecursionlimit(1000000)
 
n = int(input())

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

tmp = 0
ans = 0

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

print(ans)

Solution (Python):
n = int(input())
monster = list(map(int, input().split()))
knight = list(map(int, input().split()))
result = 0

for i in range(n):
    if knight[i] <= monster[i]:
        result += knight[i]
    elif knight[i] <= (monster[i] + monster[i+1]):
        monster[i+1] -= (knight[i] - monster[i])
        result += knight[i]
    else:
        result += (monster[i] + monster[i+1])
        monster[i+1] = 0

print(result)

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
la=list(map(int,input().split()))

ans=0
for m,i in enumerate(la):
  ans+=min(i,l[m])+l[m+1]-max(0,l[m+1]-i+min(i,l[m]))
  l[m+1]=max(0,l[m+1]-i+min(i,l[m]))
print(ans)

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

next_minus = 0
total = 0
for i in range(N):
    targets = A[i] - next_minus
    rest = B[i] - targets

    total += B[i]
    if rest > 0:
        if A[i+1] > rest:
            next_minus = rest
        else:
            next_minus = A[i+1]
            total -= (rest - A[i+1])
    else:
        next_minus = 0
    
print(total)

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

ans = 0
for i, b in enumerate(B):
    if b >= A[i]:
        ans += A[i]
        b -= A[i]
        A[i] = 0
        if b >= A[i+1]:
            ans += A[i+1]
            A[i+1] = 0
        else:
            ans += b
            A[i+1] -= b
    else:
        ans += b

print(ans)

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

count = 0

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

print(count)

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

ans = 0

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

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

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

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

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

amari=0
ans=0

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

ans+=amari
print(ans)

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

ct=0

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

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

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

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

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

    aa = A[i + 1] - bb
    if aa < 0:
        aa = 0

print(num_kill)

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

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

print(Total)

Solution (Python):
n=int(input())
a=([int(n) for n in input().split()])
b=([int(n) for n in input().split()])
b.append(0)
r=0
t=0
x=0
for i in range(n+1):
  t=b[i]+x-a[i]
  if t>=0:
    r=r+a[i]
    x=min(b[i],t)
  else:
    r=r+b[i]+x
    x=0
print(r)

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

ans = 0
for i in range(n):
    for j in range(i,i+2):
        if a[j]>0:
            x = min(a[j],b[i])
            b[i] -= x
            a[j] -= x
            ans += x

print(ans)

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

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

Solution (Python):
N = input()
A = input()
B = input()

A = [int(i) for i in A.split(" ")]
B = [int(i) for i in B.split(" ")]

count = 0
remaining_num = 0
for i in range(0, len(A)):
    if i > 0:
        if A[i] <= B[i-1]:
            count += A[i]
            B[i-1] -= A[i]
            A[i] = 0
        else:
            count += B[i-1]
            A[i] -= B[i-1]
            B[i-1] = 0
    if i < len(B):
        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):
import sys
input = sys.stdin.readline

def main():
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	b = list(map(int, input().strip().split()))
	s = 0
	for i in range(n):
		if a[i] < b[i]:
			s += a[i]
			if b[i] - a[i] < a[i+1]:
				a[i+1] -= (b[i] - a[i])
				s += b[i] - a[i]
			else:
				s += a[i+1]
				a[i+1] = 0
		else:
			s += b[i]
	print(s)

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

now = A[0]
ret = 0
for i in range(n):
    x = B[i]

    if now <= x:
        x -= now
        ret += now
        now = A[i + 1]

        if x <= 0:
            continue
        
        if now <= x:
            ret += now
            now = 0
        else:
            now -= x
            ret += x
    
    else:
        ret += x
        now = A[i + 1]

print(ret)

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

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

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

ans = 0

for i in reversed(range(num)):
  if town[i+1] >= fighter[i]:
    ans += fighter[i]
    continue
  else:
    ans += town[i+1]
    amari = fighter[i] - town[i+1]
    
  if town[i] >= amari:
    ans += amari
    town[i] -= amari
  else:
    ans += town[i]
    town[i] = 0

print(ans)

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


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

if b[n-1] - a[n-1] > 0:
    count += a[n-1]
    rest = b[n-1] - a[n-1]
    if rest < a[n]:
        count += rest
    else:
        count += a[n]
else:
    count += b[n-1]


print(count)

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

if A[0] > B[0]:
  result = B[0]
  kurikoshi = 0
else:
  result = A[0]
  kurikoshi = B[0] - A[0]  

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

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

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

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

Solution (Python):
import sys
n = int(sys.stdin.readline())
a = [int(i) for i in sys.stdin.readline().split()]
b = [int(i) for i in sys.stdin.readline().split()]

total = 0
for i, v in enumerate(b):
    m1 = min(v, a[i])

    total += m1
    a[i] -= m1

    if m1 == v:
        continue
    v -= m1

    m2 = min(v, a[i+1])
    total += m2
    a[i+1] -= m2
print(total)

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

print(cnt)

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

cnt = 0
add = 0
for i in range(n):
    tmp = b_list[i] - a_list[i]
    if tmp >= 0:
        if a_list[i+1] <= tmp:
            cnt += (a_list[i] + a_list[i+1])
            a_list[i+1] = 0
        else:
            a_list[i+1] -= tmp
            cnt += b_list[i]
    else:
        cnt += b_list[i]


print(cnt)

Solution (Python):
N = int(input())
aArr = input().split(" ")
bArr = input().split(" ")

aArr= [int(i) for i in aArr]
bArr= [int(i) for i in bArr]

res = 0

for i in range(N):
  if(bArr[i] < aArr[i]):
    res += bArr[i]
  else:
    res += aArr[i]
    if((bArr[i] - aArr[i]) < aArr[i+1]):
      res += bArr[i] - aArr[i]
      aArr[i+1] -= bArr[i] - aArr[i]
    else:
      res += aArr[i+1]
      aArr[i+1] = 0

print(res)

Solution (Python):

import sys


def solve(N: int, A: "List[int]", B: "List[int]"):
    sum = 0
    for i in range(N):
        cap = B[i]
        if cap <= A[i]:
            sum += cap
            
        else:
            sum += A[i]
            cap = cap - A[i]

            if cap <= A[i+1]:
                sum += cap
                A[i+1] = A[i+1] - cap
            else:
                sum += A[i+1]
                A[i+1] = 0

    print(sum)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [ int(next(tokens)) for _ in range(N+1) ]  
    B = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, A, B)

if __name__ == '__main__':
    main()

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

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

Solution (Python):
LARGE = 10 ** 9 + 7


def solve_a(test=False, a=2, b=16):

    if not test:
        a, b = map(int, input().split())
    if (b - a) % 2 == 0:
        print((a + b) // 2)
    else:
        print("IMPOSSIBLE")


def solve_b(test=False, n=5, p_list=[5, 2, 3, 4, 1]):

    if not test:
        n = int(input())
        p_list = list(map(int, input().split()))

    r = 0
    for i in range(n):
        if p_list[i] != i + 1:
            r += 1
    if r == 0 or r == 2:
        print("YES")
    else:
        print("NO")


def solve_c(test=False, n=2, a_list=[3, 5, 2], b_list=[4, 5]):

    if not test:
        n = int(input())
        a_list = list(map(int, input().split()))
        b_list = list(map(int, input().split()))

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

    print(res)


def solve_d(test=False, s='??2??5'):

    if not test:
        s = input()

    l = len(s)
    res_list_new = [0] * 13
    res_list_old = [0] * 13
    zero_mod = int(s.replace('?', '0')) % 13
    res_list_new[zero_mod] += 1

    for i in range(1, l + 1):
        if s[-i] == '?':
            for k in range(13):
                res_list_old[k] = res_list_new[k] % LARGE
            base_mod = pow(10, i-1, 13)
            for j in range(1, 10):
                for k in range(13):
                    res_list_new[(k + base_mod * j) % 13] += res_list_old[k]

    print(res_list_new[5] % LARGE)



















solve_c()

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

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

    if taosu < 0:
        A[i+1] = A[i+1]+taosu
        if A[i+1] < 0:
            A[i+1] = 0
    c += taosita

print(c)

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

print(k)

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

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

print(cnt)

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

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

Solution (Python):

City = int(input())

Mons = [int(a) for a in input().split()]

Blave = [int(b) for b in input().split()]


slash = 0


for i in range(City):
  if Blave[i] > Mons[i]:
    slash += Mons[i]
    Sword = Blave[i] - Mons[i]
    if Sword > Mons[i+1]:
      slash += Mons[i+1]
      Mons[i+1] = 0
    else :
      slash += Sword
      Mons[i+1] -= Sword
  else:
    slash += Blave[i]


print('{}'.format(slash))

Solution (Python):
N = int(input())
a_li = list(map(int,input().split()))
b_li = list(map(int,input().split()))

total = 0

for i in range(N):
    if(a_li[i] <= b_li[i]):
        total = total + a_li[i]
        b_li[i] = b_li[i] - a_li[i]
        if(a_li[i+1] <= b_li[i]):
            total = total + a_li[i+1]
            a_li[i+1] = 0
        else:
            total = total + b_li[i]
            a_li[i+1] = a_li[i+1] - b_li[i]
    else:
        total = total + b_li[i]

print(total)

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

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

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

if A[-1] > rest:
  temp = rest
else:
  temp = A[-1]

ans += temp

print(ans)

Solution (Python):
n = int(input())
monsters = list(reversed(list(map(int,input().split()))))
savers = list(reversed(list(map(int,input().split()))))
kotae = 0
nokori = monsters[0]

for k,i in enumerate(savers):
    a = nokori + monsters[k+1]
    if i >= a:
        kotae = kotae + a
        nokori = 0
    else:
        kotae = kotae + i
        if i >= nokori:
            nokori = a - i
        else:
            nokori = a - nokori
print(kotae)

Solution (Python):

import numpy as np

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

print(ans)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = 0
for i in range(n):
    left = min(a[i], b[i])
    b[i] -= left
    c += left
    right = min(a[i+1], b[i])
    a[i+1] -= right
    c += right
print(c)

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

p = 0
ans = 0
for ai, bi in zip(a, b):
    ans += min(-p, ai)
    n = max (ai + p, 0)
    ans += min(n, bi)
    p = min(n - bi, 0)
print(ans)

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

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

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

  else:
    d += a[i]
    b[i] = b[i] - a[i]
    
    if a[i+1] - b[i] >= 0:
      d += b[i]
      a[i+1] = a[i+1] - b[i]
      
    else:
      d += a[i+1]
      a[i+1] = 0
      
      
print(d)

Solution (Python):
def simu(i, j, c):
    if A[i] >= B[j]:
        q = B[j]
        A[i] -= B[j]
        B[j] = 0
    else:
        q = A[i]
        B[j] -= A[i]
        A[i] = 0
        if not c:
            q += simu(i + 1, j, True)
    return q


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

p = 0
for i in range(N):
    p += simu(i, i, False)

print(p)

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

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
count = 0
for i in range(N):
  for j in range(2):
    tmp = min(A[i+j],B[i])
    count += tmp
    A[i+j] -= tmp
    B[i] -= tmp
print(count)

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

M = 0

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

    else:
        M += B[i]

print(M)

Solution (Python):

def main():
	N = int(input()) 
	A = [int(f) for f in input().split()]
	B = [int(f) for f in input().split()]
	
	m = 0
	for i in range(N):
		A[i] = A[i] - B[i]
		m += B[i]
		if A[i] < 0:
			B[i] = -A[i]
			m += A[i]
			A[i] = 0
		else:
			continue
		if B[i] > 0:
			A[i+1] = A[i+1] - B[i]
			m += B[i]
			if A[i+1] < 0:
				B[i] = -A[i+1]
				m += A[i+1]
				A[i+1] = 0
	
	print(m)

if __name__ == "__main__":
	main()

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

target = a[0]
ans = 0

for i in range(n):


    if b[i] > target:

        ans += target
        res = b[i] - target
        ans += min(res, a[i+1])
        target = a[i+1] - res
        if target < 0:
            target = 0

        
        
        
    else:

        ans += b[i]
        target = a[i+1]


print(ans)

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

count = 0
am = 0
for i, b in enumerate(B):
    a = A[i]-am
    if a < 0:
        count += A[i]
        a = 0
    else:
        count += A[i]-a
    n = a-b
    if n < 0:
        count += a
        am = b-a
    else:
        count += a-n
        am = 0
i += 1
a = A[i]-am
if a < 0:
    count += A[i]
else:
    count += A[i]-a
print(count)

Solution (Python):


''' 
------------------------
author : iiou16
------------------------
'''

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

    count = 0
    nokori = 0

    for i in range(len(A)):
        mon = A[i]
        if nokori >= mon:
            count += mon
            mon = 0
        else:
            count += nokori
            mon -= nokori
        if i < N:
            if B[i] >= mon:
                count += mon
                nokori = B[i] - mon
            else:
                count += B[i]
                nokori = 0
    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
res = 0
for i in range(n+1):
    if i < n:
        res += b[i]
    ans += min(res, a[i])
    if i < n:
        res = min(max(res - a[i], 0), b[i])

print(ans)

Solution (Python):
N = int(input())
A_str = input().split()
A = [int(a) for a in A_str]
B_str = input().split()
B = [int(b) for b in B_str]

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

print(battle)

Solution (Python):
n = int(input())
aarr = list(map(int, input().split()))
barr = list(map(int, input().split()))

tmparr = aarr[:]
count = 0

for i in range(n):
    if tmparr[i] >= barr[i]:
        count += barr[i]
        tmparr[i] -= barr[i]
    else:
        count += tmparr[i]

        if tmparr[i+1] >= barr[i] - tmparr[i]:
            tmparr[i+1] -= barr[i] - tmparr[i]
            count += barr[i] - tmparr[i]
        else:
            count += tmparr[i+1]
            tmparr[i+1] = 0

print(count)

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

cnt = 0
tmp = 0
for i in range(N):
    tmp_mon = max(A[i] - tmp, 0)
    killed_mon = min(A[i], tmp)
    cnt += min(B[i], tmp_mon) + killed_mon

    if B[i] > tmp_mon:
        tmp = B[i] - tmp_mon
    elif B[i] <= tmp_mon:
        tmp = 0

cnt += min(A[-1], tmp)

print(cnt)

Solution (Python):


n = int(input())
p = input().split()
q = input().split()
a = []
b = []
a_start = 0
a_end = 0

for i in p:
    a.append(int(i))
for i in q:
    b.append(int(i))
for i in a:
    a_start += i

for i,j in enumerate(b):
    if a[i] > b[i]:
        a[i] -= b[i]
    else:
        b[i]   -= a[i]
        a[i]    = 0
        if a[i+1] > b[i]:
            a[i+1] -= b[i]
            b[i] = 0
        else:
            a[i+1] = 0
            b[i] -= a[i+1]

for i in a:
    a_end += i

print(a_start - a_end)

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

Solution (Python):
N = int(input())
aLst = list(map(int, input().split()))
bLst = list(map(int, input().split()))

ans = 0

for i in range(N):
    tmp = aLst[i] - bLst[i]
    if tmp < 0:
        if aLst[i + 1] - (bLst[i] - aLst[i]) <= 0:
            ans += aLst[i] + aLst[i + 1]
            aLst[i + 1] = 0
        else:
            ans += bLst[i]
            aLst[i + 1] = aLst[i + 1] - (bLst[i] - aLst[i])
    else:
        ans += bLst[i]
print(ans)

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

mon = 0
length = len(B) - 1

for i in range(len(B)):
	j = length - i
	if B[j] >= A[j+1]:
		mon += A[j+1]
		B[j] -= A[j+1]
		A[j+1] = 0
		if B[j] >= A[j]:
			mon += A[j]
			A[j]=0
		else:
			mon += B[j]
			A[j]-=B[j]
	else:
		mon += B[j]
		B[j] = 0
		A[j+1] -= B[j]

print(mon)