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):

"""
C - City Savers
https://atcoder.jp/contests/abc135/tasks/abc135_c

"""
import sys


def solve(N, monsters, heros):
    carries = [0] * (N + 1)
    ans = 0
    for i in range(N+1):
        m = monsters[i]
        x = min(carries[i], m)
        ans += x
        m -= x
        h = heros[i]
        y = min(h, m)
        ans += y
        if y < h:
            carries[i+1] = h - y
    return ans


def main(args):
    N = int(input())
    monsters = [int(a) for a in input().split()]
    heros = [int(b) for b in input().split()] + [0]
    ans = solve(N, monsters, heros)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[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):
    if a[i] >= b[i]:
        ans += b[i]
    else:
        ans += a[i] + min(b[i] - a[i], a[i+1])
        a[i+1] = max(0, a[i+1] - (b[i] - a[i]))

print(ans)

Solution (Python):
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    c = sum(A)
    for i in range(N):
        a = min(A[i], B[i])
        A[i] -= a
        A[i+1] -= min(A[i+1], B[i] - a)
    return c - sum(A)

print(solve())

Solution (Python):
def defeat_monster(a, b, i, j):
    defeated = min(a[i], b[j])
    a[i] -= defeated
    b[j] -= defeated
    return defeated

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

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.reverse()
b.reverse()
ans = 0
capacity = 0
for i in range(N):
    ans += min(b[i] + capacity, a[i])
    capacity = max(b[i] - max(a[i] - capacity, 0), 0)
ans += min(a[-1], capacity)
print(ans)

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

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

if A[0] > B[0]:
    ans += B[0]
    B[0] = 0
else:
    ans += A[0]
    B[0] = B[0] - A[0]

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

if A[N] > B[N - 1]:
    ans += B[N - 1]
else:
    ans += A[N]

print(ans)

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

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

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

print(res)

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

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

ans, num = 0, 0
for i in range(N):
  ans += min(B[i], A[i]+A[i+1])
  d = B[i] - A[i]
  if d > 0:
    A[i+1] -= min(d, A[i+1])
print(ans)

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

Solution (Python):
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.readline().rstrip())
    city = list(map(int, sys.stdin.readline().split()))
    brave = list(map(int, sys.stdin.readline().split()))

    init = sum(city)
    for n in range(N):
        bef = city[n]
        city[n] -= min(city[n], brave[n])
        city[n+1] -= min(city[n+1], brave[n]-min(bef, brave[n]))

    print(init-sum(city))


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

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

Sum = 0

for i in range(N):
    ai = A[i]
    ai_1 = A[i+1]
    a = ai_1 + ai
    b = B[i]
    if a<=b:
        A[i+1]=0
        Sum+=a
    else:
        if (ai - b)>0:
            Sum+=(b)
        else:
            A[i+1] = a-b
            Sum+=b
print(Sum)

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

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

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

count += min(a[-1], b[-1])

print(count)

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

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

arrA = list(map(int,input().split()))
arrB = list(map(int,input().split()))

mons = arrA[n]
res = 0
for i in range(n-1,-1,-1):
    mons += arrA[i]
    if mons>=arrB[i]:
        res += arrB[i]
        mons = min(mons-arrB[i],arrA[i])
    else:
        res += mons
        mons = 0

print(res)

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

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

print(sumMonster-sum(A))

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

ans = sum(A)

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

    if A[i] > 0 and i != N + 1:
        if A[i] - B[i] > 0:
            A[i] -= B[i]
            B[i] = 0
        else:
            B[i] -= A[i]
            A[i] = 0

print(ans - sum(A))

Solution (Python):

import sys


def solve(N: int, A: "List[int]", B: "List[int]"):
    sum=0
    temp=0
    for i in range(N):
        if B[i]<=(A[i]-temp):
            sum+=B[i]+temp
            temp=0
        else:
            if(temp>=A[i]):
                temp=B[i]
            else:
                temp=B[i]-A[i]+temp
            sum+=A[i]
    
    if(A[N]>=temp):sum+=temp
    else: sum+=A[N]
    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()))
ans = 0
for i in range(n):
    ans += min(a[i], b[i])
    
    if a[i] < b[i]:
        if a[i+1] <= b[i] - a[i]:
            ans += a[i+1]
            a[i+1] = 0
        else:
        
            a[i+1] -= b[i] - a[i]
            ans += b[i] - a[i]
        

print(ans)

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

Solution (Python):
import math
import sys

m=10**9 + 7 
sys.setrecursionlimit(1000010)
N = int(input())
A  = list(map(int,input().split()))
B  = list(map(int,input().split()))

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

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

sa = min(A[N],B[N-1])
ans = ans + sa
A[N] = A[N] - sa
B[N-1] = B[N-1] - sa
print(ans)

Solution (Python):

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

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

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

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

print(mon)

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

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

print(mon)

Solution (Python):
n = int(input())
list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))

ans = 0
re = 0

for i in range(n):
    if list_A[i] - list_B[i] - re >= 0:
        ans += list_B[i] + re
        re = 0
    else:
        ans += list_A[i]
        if list_A[i]-re >= 0:
            re = re + list_B[i] - list_A[i]
        elif list_A[i]-re < 0:
            re = list_B[i]


if list_A[n]-re >= 0:
    ans += re
else:
    ans += list_A[n]

print(ans)

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

Solution (Python):

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

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

print(ans)

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

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

Solution (Python):
from sys import stdin

n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
b = [int(x) for x in stdin.readline().rstrip().split()]

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

print(total)

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

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

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

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

print(ans)

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

print(cnt)

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

for i in range(N): 
    d = min(A[i], B[i]) 
    ans += d 
    A[i] -= d 
    B[i] -= d 
    d = min(A[i + 1], B[i]) 
    ans += d 
    A[i + 1] -= d 
    B[i] -= d 

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

print(ans)

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

D = A[-2] - B[-1]    
A[-2] = D

if A[-2] < 0:
        A[-2] = 0
        A[-1] = A[-1] - (-D)
        if A[-1] < 0:
            A[-1] = 0
Y = sum(A)
print(X-Y)

Solution (Python):


n = int(input().rstrip())

monster_list = list(map(int, input().rstrip().split(" ")))
yusya = list(map(int, input().rstrip().split(" ")))

kill_count = list(range(n))

for i in range(n):
    tmp = min(yusya[i], monster_list[i])
    if yusya[i] > tmp:
        tmp2 = min(yusya[i] - tmp, monster_list[i+1])
        monster_list[i+1] -= tmp2
        tmp += tmp2
    kill_count[i] = tmp

ans = sum(kill_count)
print(ans)

Solution (Python):
n=int(input())
b=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
f=sum(a)
for i in range(n):
    a1=a[i]
    a[i]=max(0,a[i]-b[i])
    b[i]=max(0,b[i]-a1)
    a2=a[i]
    a[i]=max(0,a[i]-b[i+1])
    b[i+1]=max(0,b[i+1]-a2)
print(f-sum(a))

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

Solution (Python):




def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split())) + [0]
    C = [0] * (N + 1)

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

    print(result)


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

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

print(sum(ans)-sum(A))

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

ans = 0
left = 0
for i in range(n):
   tmp = min(a[i], left)
   ans += tmp
   a[i] -= tmp

   if(b[i] > a[i]):
      ans += a[i]
      left = b[i] - a[i]
   else:
      ans += b[i]
      left = 0

ans += min(a[n], left)
print(ans)

Solution (Python):





import sys

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n = int(input())
    a = list(map(int,ipti().split()))
    b = list(map(int,ipti().split()))

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

    print(ans)



if __name__ == '__main__':
    main()

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

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

print(s)

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

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

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


num = 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(num - sum(B))

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

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

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

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

print(cnt)

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

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
for i in range(n):
  f = min(a[i],b[i])
  ans += f
  a[i] -= f
  g = min(a[i+1],b[i]-f)
  ans += g
  a[i+1] -= g
print(ans)

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

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

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

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

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

x = 0
y = 0
for i in range(n):
  t = min(a[i], x)
  y += t
  a[i] -= t
  if b[i] > a[i]:
    y += a[i]
    x = b[i]-a[i]
  else:
    y += b[i]
    x = 0
  
t = min(a[n], x)
y += t
print(y)

Solution (Python):
def Main(N, A, B):
    ans = 0
    remain = 0
    for i in range(N):
        if A[i] >= B[i]:
            ans += B[i]
        else: 
            ans += A[i]
            remain = B[i]-A[i]
            next_attack = A[i+1]-remain
            if next_attack > 0:
                ans += remain
                A[i+1] -= remain
            else:
                ans += A[i+1]
                A[i+1] = 0
    return ans
        

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


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
ans = 0
mons = 0
yusha = 0
for i in range(N):
    mons = max(A[i] - yusha, 0)
    ans += min(yusha, A[i])
    ans += min(mons, B[i])
    yusha = max(0, B[i] - mons)
ans += min(yusha, A[N])
print(ans)

Solution (Python):



def main():
  n = int(input())
  As = list(map(int, input().strip().split()))
  Bs = list(map(int, input().strip().split()))
  ne = sum(As)
  for i in range(n):
    if As[i] >= Bs[i]:
      As[i] -= Bs[i]
    else:
      As[i+1] = max(As[i+1] - (Bs[i] - As[i]), 0)
      As[i] = 0
  print(ne - sum(As))
    
if __name__=='__main__':
  main()

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

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

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

Solution (Python):

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

print(ans)

Solution (Python):
import sys
import itertools



read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *AB = map(int, read().split())
A = AB[:N + 1]
B = AB[N + 1:]

B.append(0)
ans = 0
rest = 0
for i, j in zip(A, B):
    x = min(rest, i)
    ans += x
    i -= x
    if i < j:
        ans += i
        rest = j - i
    else:
        ans += j
        rest = 0
print(ans)

Solution (Python):
import sys
import itertools



read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *AB = map(int, read().split())
A = AB[:N + 1]
B = AB[N + 1:]

ans = 0
cnt = 0
for i, j in zip(A, B):
    
    ans += min(i, cnt)
    m = min(i-cnt, j)
    m=max(m,0)
    ans += m
    cnt = max(j - m, 0)
ans += min(A[-1], cnt)

print(ans)

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

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

print(c)

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

print(dead_mon)

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

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LI()
    B=LI()
    ans = 0
    for i,b in enumerate(B):
        if A[i]<=b:
            ans += A[i]
            b -= A[i]
            ans += max(0,min(A[i+1],b))
            A[i+1] = max(0,A[i+1]-b)
        else:            
            ans += b
            b = 0
    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):
    a_t=min(b[i],a[i])
    ans+=a_t
    b[i]-=a_t
    b_t=min(a[i+1],b[i])
    a[i+1]-=b_t
    ans+=b_t
print(ans)