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

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

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

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
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) :
    sA = A[i] + A[i+1]
    if sA >= B[i] :
        d = B[i] - A[i]
        if d >= 0:
            A[i] = 0
            A[i+1] -= d
        else :
            A[i] -= B[i]
        ans += B[i]
    else :
        A[i] = 0
        A[i+1] = 0
        ans += sA
print(ans)

Solution (Python):

import sys


def solve(N: int, A: "List[int]", B: "List[int]"):
    B.append(0)
    ans = 0
    tmp = 0

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

    print(ans)

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

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

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

    if ex != 0:
        if a[i+1] < ex:
            knock += a[i+1]
            a[i+1] = 0
        else:
            a[i+1] -= ex
            knock += ex

print(knock)

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

print(Ans)

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
B.append(0)
sumA=sum(A)
c=min(A[0],B[0])
A[0]-=c
B[0]-=c

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

print(sumA-sum(A))

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

sum_v = 0

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



print(sum_v)

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

print(ans)

Solution (Python):



import sys
input = sys.stdin.readline


def solve(N, A, B):
    defeat_monstar = 0
    for nn in range(N):
        
        if A[nn] >= B[nn]:
            defeat_monstar += B[nn]
        else:
            B[nn] -= A[nn]
            
            if A[nn + 1] <= B[nn]:
                defeat_monstar += A[nn] + A[nn + 1]
                A[nn + 1] = 0
            else:
                A[nn + 1] = A[nn + 1] - B[nn]
                defeat_monstar += A[nn] + B[nn]

    return defeat_monstar


N = int(input())


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








print(solve(N, A, B))

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

ans = 0

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

Solution (Python):
import sys
input = sys.stdin.readline
from functools import reduce

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

    def save(i):
        n = min(A[i], B[i])
        m = 0
        if B[i] > A[i]:
            m = min(A[i+1], B[i] - A[i])
            A[i+1] -= m
        return n+m

    print(reduce(lambda total, i: total + save(i), range(N), 0))


main()

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

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

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

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

print(cnt)

Solution (Python):
N = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
a_list.reverse()
b_list.reverse()
total = 0
res = 0

for i in range(len(a_list)):
    if i == 0:
        val_1 = a_list[0]
        val_2 = b_list[0]
        if val_1 <= val_2: 
            total += val_1
            res = val_2 - val_1
        else:
            total += val_2
            res = 0
            
    elif i == len(a_list) - 1:
        if res >= a_list[i]:
            total += a_list[i]
        else:
            total += res
        
    else:
        val_1 = a_list[i]
        val_2 = b_list[i] + res
        if  val_1 <= val_2: 
            total += val_1
            res = val_2 - val_1
            if res > b_list[i]:
                res = b_list[i]
        else:
            total += val_2
            res = 0
            
print(total)

Solution (Python):


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

Solution (Python):


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

b.append(0)
z = 0
c = b[0]
for i in range(n+1):
    x = a[i] - c
    
    if i < n:
        if x <= 0:
            z += a[i]
            
            if -x >= a[i+1]:
                c = b[i+1] + a[i+1]
            elif -x < a[i+1]:
                c = b[i+1] - x
        elif x > 0:
            z += c
            
            c = b[i+1]
    elif i == n:
        if x <= 0:
            z += a[i]
            
        else:
            z += c
            
print(z)

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

print(r - sum(a))

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

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

    else:
        ans+=A[i]+A[i+1]
        A[i]=0
        A[i+1]=0

print(ans)

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

    right = min(A[i+1], B[i])
    ans += right
    A[i+1] -= right
    B[i] -= right
print(ans)

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

ans = 0
p = 0

for index, i in enumerate(A):
  if index > len(B) - 1:
    if p > A[index]:
      ans += A[index]
    else:
      ans += p
      
    break

  if A[index] - (B[index] + p) < 0:
    ans += A[index]
    if p > A[index]:
      p = B[index]
    else:
      p = abs(A[index] - B[index] - p)
      
    continue

  ans += B[index] + p
  p = 0

print(ans)

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

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

Solution (Python):
N = int(input())
Monster = list(map(int, input().split()))
Brave = list(map(int, input().split()))
cnt = 0
amari = 0

for i in range(N):

    cnt += min(amari, Monster[i])
    Monster[i] = max(0, Monster[i]-amari)
    cnt += min(Monster[i], Brave[i])
    amari = max(0, Brave[i]-Monster[i])

cnt += min(amari, Monster[N])
print(cnt)

Solution (Python):

import sys
import math
import os
import itertools
import _collections
import string
from functools import lru_cache

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

MOD = int(1e09) + 7

def main():
    N = cin.int()
    A = cin.mapInt()
    B = cin.mapInt()

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

if __name__ == "__main__":
    main()

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

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

Solution (Python):
def main():
    n = int(input())
    machi_list = list(map(int, input().split()))
    yusha_list = list(map(int, input().split()))

    init_sum = sum(machi_list)

    for i in range(n):
        machi_list[i] -= yusha_list[i]
        if machi_list[i] < 0:
            machi_list[i+1] += machi_list[i]
            if machi_list[i+1] < 0:
                machi_list[i+1] = 0
            machi_list[i] = 0

    print(init_sum-sum(machi_list))

if __name__ == '__main__':
    main()

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

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

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

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

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

print(sum)

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
first=a.pop(0)
ans=0
for i in range(n):
  if first+a[i]-b[i] <= 0:
    ans+=first+a[i]
    first=0
  elif first-b[i]>=0:
    ans+=b[i]
    first=a[i]
  else:
    ans+=b[i]
    first=a[i]-(b[i]-first)
print(ans)

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

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

Solution (Python):
f = lambda : list(map(int, input().split()))
n = int(input())
la = f()
lb = f()

tmp = 0
ans = 0

for i in range(n):
  m = min(la[i], tmp+lb[i])
  tmp = min(tmp+lb[i]-m, lb[i])
  ans += m

print(ans + min(la[n], tmp))

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

monsters = [int(i) for i in input().split()]
savers = [int(i) for i in input().split()]

cnt = 0
for i in range(N):
    first_match = savers[i] - monsters[i]
    if first_match > 0:
        cnt += monsters[i]
        monsters[i] = 0
        
        second_match = first_match - monsters[i+1]
        
        if second_match >= 0:
            cnt += monsters[i+1]
            monsters[i+1] = 0
        else :
            cnt += first_match
            monsters[i+1] = abs(second_match)
    else :
        cnt += savers[i]
        monsters[i] = abs(first_match)

print(cnt)

Solution (Python):
n = int(input())
monster = list(map(int, input().split()))
savers = list(map(int, input().split()))
first = sum(monster)
rest = []
for i in reversed(range(n)):
    monster_i = monster.pop()
    save_i = savers[i]
    
    monster_ii = max(monster_i - save_i, 0)
    
    save_ii = save_i - (monster_i - monster_ii)
    rest.append(monster_ii)
    monster[-1] = max(monster[-1] - save_ii, 0)
ans = first - sum(rest) - monster[0]
print(ans)

Solution (Python):
n = int(input())
al = [int(i) for i in input().split()]
bl = [int(i) for i in input().split()]

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

Solution (Python):
n=int(input())
ai=[]
for i in map(int,input().split()):
    ai.append(i)
bi=[]
for i in map(int,input().split()):
    bi.append(i)

asum=0
for i in range(n+1):
    asum=asum+ai[i]    

i=0
if ai[i]<=bi[i]:
    bi[i]=bi[i]-ai[i]
    ai[i]=0
else :
    ai[i]=ai[i]-bi[i]
    bi[i]=0
for i in range(1,n,1):
    if ai[i]<=bi[i-1]:
        bi[i-1]=bi[i-1]-ai[i]
        ai[i]=0
    else :
        ai[i]=ai[i]-bi[i-1]
        bi[i-1]=0
    if ai[i]<=bi[i]:
        bi[i]=bi[i]-ai[i]
        ai[i]=0
    else :
        ai[i]=ai[i]-bi[i]
        bi[i]=0
i=n
if ai[i]<=bi[i-1]:
    bi[i-1]=bi[i-1]-ai[i]
    ai[i]=0
else :
    ai[i]=ai[i]-bi[i-1]
    bi[i-1]=0

asum2=0
for i in range(n+1):
    asum2=asum2+ai[i]    

print(asum-asum2)

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

Sum = sum(a)

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

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

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


ans=t-sum(a)        
        
print(ans)

Solution (Python):
def LI():
  return [int(s) for s in input().split()]

N = int(input())

A_s = LI()
B_s = LI()

counter = 0

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

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

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

zan = a[0]
kill = 0
for i in range(n):
    kill_first_town = min(zan, b[i])
    if kill_first_town == zan:
        kill_second_town = min(b[i]-zan, a[i+1])
    else:
        kill_second_town = 0
    zan = a[i+1] - kill_second_town    
    kill += kill_first_town + kill_second_town
print(kill)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
before=sum(A)
for i in range(N):
    temp = A[i]
    A[i] = max(0,A[i]-B[i])
    B[i] = max(0,B[i]-temp)
    A[i+1] = max(0,A[i+1]-B[i])
after=sum(A)
print(before-after)

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

before=sum(A)
for n in range(N):
    temp = A[n]
    A[n] = max(0, A[n]-B[n])
    B[n] = max(0, B[n]-temp)
    A[n+1] = max(0, A[n+1]-B[n])
after=sum(A)
print(before-after)

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

cnt = 0
for i in range(len(b_nums)):
    b_num = b_nums[i]
    m_num = m_nums[i]
    cnt += min(b_num, m_num)
    if b_num > m_num:
        tmp = b_num - m_num
        if tmp > m_nums[i+1]:
            cnt += m_nums[i+1]
            m_nums[i+1] = 0
        else:
            cnt += tmp
            m_nums[i+1] -= tmp

print(cnt)

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

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

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

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

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


    print(ans)


if __name__=="__main__":
    main()

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

for i in range(N):
  num = min(A[i], B[i])
  A[i] -= num
  B[i] -= num
  
  if B[i]:
    num = min(A[i+1], B[i])
    A[i+1] -= num
    
print(sumA - sum(A))

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

print(count)

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

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

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
ai = list(map(int, input().split()))
bi = list(map(int, input().split()))

ans = 0
for i in range(n):
    ans += min(ai[i], bi[i])
    ans += min(ai[i+1], bi[i]-min(ai[i], bi[i]))
    ai[i+1] -= min(ai[i+1], bi[i]-min(ai[i], bi[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):
    ans += min(A[i],B[i])
    if B[i]>A[i]:
        sarani = min(A[i+1],B[i]-A[i])
        A[i+1] -= sarani
        ans += sarani
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):
    ans += min(A[i],B[i])
    if B[i]>A[i]:
        sarani = min(A[i+1],B[i]-A[i])
        A[i+1] -= sarani
        ans += sarani
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):
    
    
    if b[i]==a[i]:
        count+=b[i]
    elif b[i] > a[i]:
        count+=a[i]
        if b[i]-a[i] == a[i+1]:
            count+=(b[i]-a[i])
            a[i+1]=0
        elif b[i]-a[i] > a[i+1]:
            count+=a[i+1]
            a[i+1]=0
        elif b[i]-a[i] < a[i+1]:
            count+=(b[i]-a[i])
            a[i+1]-=(b[i]-a[i])
    elif b[i] < a[i]:
        count+=b[i]

print(count)

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

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

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

Solution (Python):
import numpy as np
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
sum=np.sum(b)
for i in reversed(range(n)):
  k = a[i+1]
  l = a[i]
  m = b[i]
 
  a[i+1] = max(k-m, 0)
  b[i] = max(m-k, 0)
 
  a[i] = max(l-b[i], 0)
  b[i] = max(b[i]-l, 0)
postsum=np.sum(b)
print(sum-postsum)

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

for i in range(n):
    if A[i] > B[i]:
        rs += B[i]
    else:
        t = min(B[i]-A[i], A[i+1])
        A[i+1] -= t
        rs += t + A[i]
print(rs)

Solution (Python):

import numpy as np
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
sum=np.sum(b)
for i in reversed(range(n)):
  k = a[i+1]
  l = a[i]
  m = b[i]

  a[i+1] = max(k-m, 0)
  b[i] = max(m-k, 0)

  a[i] = max(l-b[i], 0)
  b[i] = max(b[i]-l, 0)
postsum=np.sum(b)
print(sum-postsum)

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

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

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

killed = 0
for i in range(n):
    now_kill = min(a[i],b[i])
    killed += now_kill
    a[i] -= now_kill
    b[i] -= now_kill
    if b[i] == 0:continue
    now_kill = min(a[i+1],b[i])
    killed += now_kill
    a[i+1] -= now_kill
    b[i] -= now_kill

print(killed)

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

    b = min(A[i + 1], B[i])
    ans += b
    
    A[i + 1] -= b
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

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

Solution (Python):
import sys

def main():
    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)

main()