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(z) for z in input().split()]
B = [int(w) for w in input().split()]

defeat = 0

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

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

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

print(score)

Solution (Python):

def main():
    n = int(input())
    towns = list(map(int,input().split()))
    braves = list(map(int,input().split()))

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

        if braves[b]:
            if towns[b] > braves[b]:
                count = count + braves[b]
                towns[b] = towns[b] - braves[b]
                braves[b] = 0
                
            else:
                count = count + towns[b]
                braves[b] = braves[b] - towns[b]
                towns[b] = 0

    print(count)

if __name__ == "__main__":
    main()

Solution (Python):
import sys

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

def main():
    N = INT()
    A = LIST()
    B = LIST()
    rest = 0
    count = 0
    for i in range(N):
        if A[i] <= rest:
            count += A[i]
            tmp = 0
        else:
            count += rest
            tmp = A[i] - rest
        if tmp <= B[i]:
            count += tmp
            rest = B[i] - tmp
        else:
            count += B[i]
            rest = 0
    if A[-1] <= rest:
        count += A[-1]
    else:
        count += rest
    print(count)

if __name__ == '__main__':
    main()

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

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

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

defeat = 0
for i in range(n-1, -1, -1):
  if monsters[i+1] >= heros[i]:
    defeat += heros[i]
  else:
    defeat += monsters[i+1]
    heros[i] -= monsters[i+1]
    if monsters[i] >= heros[i]:
      defeat += heros[i]
      monsters[i] -= heros[i]
    else:
      defeat += monsters[i]
      monsters[i] = 0
      
print(defeat)

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

print(ans)

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

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

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]
        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())
mati = list(map(int,input().split()))
yuu = list(map(int,input().split()))
monster = 0
for i in range(N,0,-1):
    x = mati[i] + mati[i-1]
    if x <= yuu[i-1]:
        monster += x
        mati[i],mati[i-1] = 0,0
    else:
        monster += yuu[i-1]
        if yuu[i-1] >= mati[i]:
            mati[i-1] += -(yuu[i-1]-mati[i])
            mati[i] = 0
        else:
            mati[i] = mati[i] - yuu[i-1]
print(monster)

Solution (Python):
import numpy as np

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


result = 0

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

Solution (Python):


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

rest = [0] * N
score = [0] * (N + 1)

for i in range(0, N + 1):
    if i == 0:
        if A[i] < B[i]:
            score[i] = A[i]
            rest[i] = B[i] - A[i]
        else:
            score[i] = B[i]
            rest[i] = 0
    elif i == N:
        score[i] = min([A[i], rest[i - 1]])
    else:
        if (A[i] - rest[i - 1]) < B[i]:
            score[i] = A[i]
            rest[i] = min([B[i] - A[i] + rest[i - 1], B[i]])
        else:
            score[i] = B[i] + rest[i - 1]
            rest[i] = 0
total = 0
for i in score:
    total = total + i

print total

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

B.append(0)
k = 0
answer = 0

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

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


def count(A, B):
    ret = 0
    for i in range(N - 1, -1, -1):
        m = min(A[i + 1], B[i])
        B[i] -= m
        ret += m
        m = min(B[i], A[i])
        A[i] -= m
        ret += m
    return ret


print(max(count(A, B), count(A[::-1], B[::-1])))

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

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

Solution (Python):
cn = int(input())
enm = input().split(" ")
brv = input().split(" ")
taoshita = int(0)

for i in range(0,cn):
    taoshita = taoshita + min(abs(int(brv[i])),abs(int(enm[i])))  
    enm[i] = int(enm[i]) - int(brv[i])
    if int(enm[i]) < 0:
        brv[i] = abs(int(enm[i]))
        enm[i] = 0
        if (int(enm[i+1]) - int(brv[i])) < 0:
            taoshita = taoshita + int(enm[i+1])
            enm[i+1] = 0
        if (int(enm[i+1]) - int(brv[i])) >= 0:
            enm[i+1] = int(enm[i+1]) - int(brv[i])
            taoshita = taoshita + int(brv[i])
print(taoshita)

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

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

cnt += a[n] if amari >= a[n] else amari

print(cnt)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
n=int(input())
aa=list(map(int,input().split()))
bb=list(map(int,input().split()))
s = 0
for i,b in enumerate(bb):
    t = min(aa[i]+aa[i+1], b)
    s += t
    if aa[i] >= t:
        aa[i] -= t
    else:
        t -= aa[i]
        aa[i] = 0
        aa[i+1] -= t
print(s)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
n=0
m=0
for i in range(N):
	n=n+int(min(A[i]+A[i+1],B[i]))
	if A[i]<=B[i] and A[i]+A[i+1]>=B[i]:
		A[i+1]=A[i+1]-(B[i]-A[i])
	elif A[i]<=B[i] and A[i]+A[i+1]<=B[i]:
		A[i+1]=0
for i in reversed(range(N)):
	m=m+int(min(A[i]+A[i+1],B[i]))
	if A[i+1]<=B[i] and A[i+1]+A[i]>=B[i]:
		A[i]=A[i]-(B[i]-A[i+1])
	elif A[i+1]<=B[i] and A[i+1]+A[i]<=B[i]:
		A[i]=0
print(max(m,n))

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

print(ans)

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

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

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

ans = B[:] + [0]

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

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

ans = B[:] + [0]

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

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

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

print(count)

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

C_kill = 0
try:
    for i in range(N):
        C_kill = C_kill + min(A[i], B[i])
        B[i] = max(0, B[i]-A[i])
        C_kill = C_kill + min(A[i+1], B[i])
        A[i+1] = max(0, A[i+1] - B[i])
except:
    print("ERROR")

print(C_kill)

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

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

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

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

score = 0

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

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

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

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

N = int(input())
A = [int(v) for v in input().split()]
B = [int(v) for v in input().split()]
s = [0] * (N + 1)

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

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

Solution (Python):
n = int(input())
monsters = [int(value) for value in input().split()]
heros = [int(value) for value in input().split()]


defeatable_monsters = 0


rev_monsters = monsters[::-1]
rev_heros = heros[::-1]

for offset, hero in enumerate(rev_heros):
    if hero > rev_monsters[offset]:
        defeatable_monsters += rev_monsters[offset]
        hero -= rev_monsters[offset]
        rev_monsters[offset] = 0
    else:
        defeatable_monsters += hero
        rev_monsters[offset] -= hero
        hero = 0
        continue

    if hero > rev_monsters[offset+1]:
        defeatable_monsters += rev_monsters[offset+1]
        hero -= rev_monsters[offset+1]
        rev_monsters[offset+1] = 0
    else:
        defeatable_monsters += hero
        rev_monsters[offset+1] -= hero
        hero = 0


print(defeatable_monsters)

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

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

B.append(int(0))    

TOT = sum ( A )

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

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

ans = 0

for index, brave in enumerate(b):
    ans += min(a[index] + a[index+1], brave)
    a[index+1] -= max(0, brave - a[index])
    if a[index+1] < 0:
        a[index+1] = 0

print(ans)

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

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

print(sum(ans))

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

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

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

import copy
cnt=0
BB = copy.deepcopy(B) + ([[0,0],[0,0]])
for i, ai in enumerate(A):
    if sum(BB[i]) > ai:
        cnt += ai
        if BB[i][0] > ai:
            BB[i+1][0] += BB[i][1]
        else:
            BB[i+1][0] += sum(BB[i]) - ai
    else:       
        cnt += sum(BB[i])

print(cnt)

Solution (Python):
n = int(input())
m = list(map(int, input().split()))
s = list(map(int, input().split()))
atk = 0
for i, e in enumerate(s):
    rem = e
    am = m[i]
    am2 = m[i+1]
    m[i] = max(0, am - rem)
    rem -= am - m[i]
    m[i+1] = max(0, am2 - rem)
    rem -= am2 - m[i+1]
    atk += e - rem
print(atk)

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

for i in range(N):
  if A_list[i] + A_list[i+1] <= B_list[i]:
    kill_cnt += (A_list[i] + A_list[i+1])
    A_list[i]=0
    A_list[i+1]=0
  else:
    kill_cnt += B_list[i]
    if A_list[i]>=B_list[i]:
      A_list[i]=A_list[i]-B_list[i]
    else:
      A_list[i+1] = A_list[i+1] - (B_list[i]-A_list[i])
      
print(kill_cnt)

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

print(count)

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

Solution (Python):
n = int(input())
monster = list(map(int, input().split()))
brave = list(map(int, input().split()))

cnt = 0

for i in range(n):
    kill1 = min(monster[i], brave[i])
    monster[i] -= kill1
    brave[i] -= kill1

    kill2 = min(monster[i+1], brave[i])
    monster[i+1] -= kill2
    brave[i] -= kill2

    cnt += (kill1+kill2)

print(cnt)

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

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

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

print(count)

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

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

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

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

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
a = A[0]
for i in range(N):
    a_next = A[i+1]
    b = B[i]

    ans += min(a, b)

    if b > a:
        ans += min(a_next, (b-a))
        a_next -= min(a_next, (b-a))

    a = a_next

print(ans)

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

cnt = 0
for i in range(N):
    a = a_list[i]
    b = b_list[i]
    if a >= b:
        cnt += b
    else:
        cnt += a
        kill = min(a_list[i+1], b-a)
        cnt += kill
        a_list[i+1] -= kill
        
print(cnt)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]


def main():

  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)


if __name__ == '__main__':
    main()

Solution (Python):

def main():

  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)


if __name__ == '__main__':
    main()

Solution (Python):

def main():

  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)


if __name__ == '__main__':
    main()

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

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

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

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

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

res = 0
for i, b in enumerate(b_list):
    res += min(a_list[i], b)
    res += min(a_list[i+1], b-min(a_list[i],b))
    a_list[i+1] -= min(b-min(a_list[i],b), a_list[i+1])
print(res)

Solution (Python):
from collections import deque
from sys import stdin

N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
B = [int(_) for _ in stdin.readline().rstrip().split()]

ans = 0

for i, b in enumerate(B):
    ans += min(A[i], b)
    b -= min(A[i], b)
    ans += min(A[i+1], b)
    A[i+1] -= min(b, A[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):
    ni0 = min(a[i], b[i])
    ni1 = min(a[i+1],b[i]-ni0)
    a[i] -= ni0
    a[i+1] -= ni1
    count += ni0+ni1

print(count)

Solution (Python):
n = int(input())
monsters = [int(x) for x in input().rstrip("\n").split(" ")]
damages = [int(x) for x in input().rstrip("\n").split(" ")]
total_monsters = sum(monsters)
result = 0
remain = monsters[n]
for i in range(n-1,-1,-1):
  
  damage = damages[i]
  monster = monsters[i]
  
  if damage <= remain:
    result += damage
    remain = monster
  else:
    damage -= remain
    result += remain
    if monster >= damage:
      remain = monster - damage
      result += damage
    else:
      result += monster
      remain = 0
print(result)

Solution (Python):

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

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

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

print(ans)

Solution (Python):
n = int(input())
monster = list(map(int,input().split()))
brave = list(map(int,input().split()))
ans = 0
for i in range(n):
  kill = min(monster[i],brave[i])
  ans += kill
  brave[i] -= kill
  kill = min(monster[i+1],brave[i])
  ans += kill
  monster[i+1] -= kill
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):



from collections import deque

N = int(input())
mon = deque(list(map(int, input().split())))
pla = deque(list(map(int, input().split())))
array = []
a, b, c = 0, 0, 0

for i in range(N):
    a = mon.popleft()
    b = pla.popleft()
    if a < b:
        array.append(a)
        array.append(min((b - a), mon[0]))
        mon[0] -= min((b - a), mon[0])
    else:
        array.append(b)

print(sum(array))

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


ans=0
for i in range(n):
    if a[i]<b[i]:
        b[i]-=a[i]
        ans+=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):
import math
N = int(input())
monsters = list(map(int, input().split()))
aaaa = list(map(int, input().split()))
totalMonsters=sum(monsters)
aliveMonsters = monsters[0]
for i in range(N):
    monsters[i], aaaa[i] = max(monsters[i]-aaaa[i], 0), max(aaaa[i] - monsters[i], 0)
    monsters[i+1] = max(monsters[i+1]-aaaa[i], 0)
print(totalMonsters-sum(monsters))

Solution (Python):


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

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

    atk = B[i] if B[i] <= A[i+1] else A[i+1]
    A[i+1] = A[i+1] - atk
    B[i] = B[i] - atk
    res = res + atk

print(res)