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

ans = 0
for i in range(N):
    if A[i] <= B[i]:
        ans += A[i]
        B[i] -= A[i]
        A[i] = 0
        if A[i + 1] <= B[i]:
            ans += A[i + 1]
            B[i] -= A[i + 1]
            A[i + 1] = 0
        else:
            ans += B[i]
            A[i + 1] -= B[i]
    else:
        ans += B[i]
        B[i] = 0
        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):
    left = min(A[i], B[i])
    A[i] -= left
    B[i] -= left
    ans += left
    right = min(A[i + 1], B[i])
    A[i + 1] -= right
    B[i] -= right
    ans += right

print(ans)

Solution (Python):
n = int(input())
list_a = list(map(int,input().split()))
list_b = list(map(int,input().split()))
ans1 = sum(list_a)
dum1 = 0
for i in range(n-1,-1,-1):
    dum1 = list_a[i+1] - list_b[i]
    list_a[i+1] = dum1
    if list_a[i+1] < 0:
        list_a[i+1] = 0
    if dum1 < 0:
        list_a[i] = list_a[i] + dum1
        if list_a[i] < 0:
            list_a[i] = 0
ans2 = sum(list_a)

print(ans1-ans2)

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 b[i] >= a[i]:
        res+=a[i]
        t = b[i] - a[i]
        res+=min(t,a[i+1])
        a[i+1] = max(0,a[i+1] - t)

    else:
        res+=b[i]

print(res)

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

count=0
for i in range(0,N):
  y=B[i]-A[i]
  if y>=0:
    count+=A[i]
    z=y-A[i+1]
    if z>=0:
      count+=A[i+1]
      A[i+1]=0
    else:
      count+=y
      A[i+1]-=y
  else:
    p=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
for i in range(N):
    t = min(A[i], B[i])
    ans += t
    A[i] -= t
    B[i] -= t
    s = min(A[i+1], B[i])
    ans += s
    A[i+1] -= s
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]
		b[i] -= a[i]
		if b[i] >= a[i+1]:
			c += a[i+1]
			a[i+1] = 0
		else:
			c += b[i]
			a[i+1] -= b[i]
	else:
		c += b[i]
print (c)

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

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

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

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

for i in range(N):
    a[i]-=b[i]

    if  a[i]<=0:
        a[i+1]+=a[i]
        if a[i+1]<=0:
            a[i+1]=0
        a[i]=0

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

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

killed = 0

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

print(killed)

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):
    for j in range(2):
        temp = min(a_list[i + j], b_list[i])
        ans += temp
        a_list[i + j] -= temp
        b_list[i] -= temp
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 += min(A[i]+A[i+1],B[i])
    A[i+1] = max(0,A[i+1] - (B[i] -A[i]))
    A[i] = 0
  else:
    ans += B[i]
    A[i] -= B[i]
  
print(ans)

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

r = 0
result = 0

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

if a[n] < r:
    result += a[n]
else:
    result += r
        
print(result)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
death = 0
tmp = 0
for i,x in enumerate(b):
    if i == 0:
        death += min(a[i]+a[i+1],b[i])
        if (a[i]+a[i+1])<=b[i]:
            tmp = 0
        else:
            if a[i]>=b[i]:
                tmp = a[i+1]
            else:
                tmp = a[i]+a[i+1]-b[i]
    else:
        death += min(tmp+a[i+1],b[i])
        if (tmp+a[i+1])<=b[i]:
            tmp = 0
        else:
            if tmp>=b[i]:
                tmp = a[i+1]
            else:
                tmp = tmp+a[i+1]-b[i]
    
print(death)

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

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

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

print(cnt)

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

print(ans)

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N = i_i()
A = i_l()
B = i_l()

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

    if A[i] < B[i]:
        if A[i+1] >= B[i] - A[i]:
            ans += B[i] - A[i]
            A[i+1] = A[i+1] - (B[i] -A[i])
        else:
            ans += A[i+1]
            A[i+1] = 0

print(ans)

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N = i_i()
A = i_l()
B = i_l()

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

cnt = 0
res = 0 
for i in range(n):

    ch = min(a[i],res)
    cnt += ch
    a[i] -= ch
    if b[i] > a[i]:
        cnt += a[i]
        res = b[i] - a[i]
    else:
        cnt += b[i]
        res = 0

cnt += min(a[n],res)

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]
    else:
        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
print(ans)

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

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

kill = 0
for x in range(len(B)):
    next_town_kill = 0
    same_town_kill = min(B[x], A[x])
    kill += same_town_kill

    remain = B[x] - A[x]
    if remain > 0:
        next_town_kill = min(remain, A[x + 1])
        kill += next_town_kill
        A[x + 1] = max(A[x + 1] - remain, 0)
print(kill)

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

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

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

BeforeA = sum(A)

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

AfterA = sum(A)

print(BeforeA - AfterA)

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

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

  else:
    k+=b[i]
    a[i]=a[i]-b[i]
    b[i]=0

print(k)

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

Solution (Python):
import numpy as np
import sys

n=int(sys.stdin.readline())
a=list(map(int,sys.stdin.readline().split()))
b=list(map(int,sys.stdin.readline().split()))
pre=0
cnt=0
for i in range(n):
    if a[i] <= pre:
        cnt+=a[i]
        pre=b[i]
    else:
        a1=a[i]-pre
        cnt+=pre
        if a1<=b[i]:
            cnt+=a1
            pre=b[i]-a1
        else:
            cnt+=b[i]
            pre=0
cnt+=min(pre,a[n])
print(cnt)

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

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

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

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


if __name__ == "__main__":
    main()

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N = INT()
A = LIST()
B = LIST()
A_before = sum(A)

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

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

Solution (Python):
def myAnswer(N:int,A:list,B:list) -> int:
   total = 0
   for i in range(N):
      if(A[i] < B [i]):
         total += A[i]
         if(A[i + 1] > B[i] - A[i]):
            A[i+1] -= B[i] - A[i]
            total += B[i] - A[i]
         else:
            total += A[i + 1]
            A[i + 1] = 0
         A[i] = 0
      else:
         A[i] -= B[i]
         total += B[i]
   return total

def modelAnswer():
   tmp=1
def main():
   N = int(input())
   A = list(map(int,input().split()))
   B = list(map(int,input().split()))
   print(myAnswer(N,A[:],B[:]))

if __name__ == '__main__':
   main()

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

Solution (Python):


int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    n = II()
    A = LI()
    B = LI()

    cnt = 0
    for i in range(n):
        left = min(A[i], B[i])
        B[i] -= left
        cnt += left

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

if __name__ == '__main__':
    solve()

Solution (Python):


int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    n = II()
    A = LI()
    B = LI()

    cnt = 0
    pre = A[-1]
    for i in range(n-1, -1, -1):
        ai = pre
        aj = A[i]
        b = B[i]
        
        if ai >= b:
            cnt += b
            pre = aj
        else:   
            cnt += ai
            b = b - ai
            if aj >= b:
                cnt += b
                pre = aj - b
            else:
                cnt = cnt + aj
                pre = 0
    print(cnt)






if __name__ == '__main__':
    solve()

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

print(total)

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

kill=0
a1=a[0]
for i in range(N):
    if a1 >= b[i]:
        kill += b[i]
        a1 = a[i+1]
    elif a1+a[i+1] >= b[i]:
        kill += b[i]
        a1 = a[i+1]+a1-b[i]
    else:
        kill += a1 +a[i+1]
        a1 = 0
print(kill)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1

""" Input
S = input().rstrip()
N, A, B = map(int, input().split())
A = np.array(input().split(), dtype=np.float64)
D = [int(input()) for _ in range(N)]
AB = [[int(x) for x in input().split()] for _ in range(N)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]
"""

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

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


print(total - sum(A))

Solution (Python):

import io
import math

data=int(input())

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

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

counter=0

for i in range(data):
	counter+=min(nim[i],mike[i])
	mike[i]=max(mike[i]-nim[i],0)
	counter+=min(nim[i+1],mike[i])
	nim[i+1]=max(0,nim[i+1]-mike[i])

print(counter)

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

Solution (Python):
import sys

n = int(sys.stdin.readline().rstrip())
*a, = map(int, sys.stdin.readline().split())
*b, = map(int, sys.stdin.readline().split())

def main():
    tot = 0
    for i in range(n):
        tot += min(b[i], a[i])
        b[i] = max(0, b[i] - a[i])
        tot += min(b[i], a[i+1])
        a[i+1] = max(0, a[i+1] - b[i])
    print(tot)
        
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]:
        cnt+=B[i]
    else:
        cnt+=A[i]
        B[i]-=A[i]
        if B[i]<A[i+1]:
            cnt+=B[i]
            A[i+1]-=B[i]
        else:
            cnt+=A[i+1]
            A[i+1]=0
print(cnt)

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):


import sys



















def main():
    mod = 1000000007                  
    inf = float('inf')
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    n = ii()
    a = lmi()
    b = lmi()
    ans = 0
    for i in range(n):
        if a[i] >= b[i]:
            ans += b[i]
        else:
            ans += a[i]
            rest_power = b[i] - a[i]
            ans += min(a[i+1], rest_power)
            a[i+1] = max(0, a[i+1] - rest_power)
    print(ans)

if __name__ == "__main__":
    main()

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

Solution (Python):










mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    A = readInts()
    B = readInts()
    ans = 0
    for i in range(n):
        prv = A[i] + A[i+1] 
        if prv - B[i] >= 0:
            if A[i] - B[i] >= 0:
                ans += B[i]
            else:
                ans += B[i]
                A[i+1] = prv - B[i]
        else:
            ans += prv
            A[i+1] = 0
    print(ans)
if __name__ == '__main__':
  main()

Solution (Python):
N = int(input())
monsters = list(map(int, input().split()))
heroes = list(map(int, input().split()))

damage = 0

for i in range(N):
    if monsters[i] + monsters[i + 1] <= heroes[i]:
        damage += monsters[i] + monsters[i + 1]
        monsters[i + 1] = 0
    elif monsters[i] < heroes[i]:
        damage += heroes[i]
        monsters[i + 1] -= heroes[i] - monsters[i]
    else:
        damage += heroes[i]

print(damage)

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

defeat = lambda Ai, Bi: min(Ai, Bi) 

total = 0
for i in range(len(B)):
    num_of_defeated = defeat(A[i], B[i])
    A[i] -= num_of_defeated
    total += num_of_defeated

    remainder = B[i] - num_of_defeated
    num_of_defeated = defeat(A[i + 1], remainder)
    A[i + 1] -= num_of_defeated
    total += num_of_defeated

print(total)

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

total = 0
for i in range(len(B)):
    num_of_defeated = min(A[i], B[i])
    A[i] -= num_of_defeated
    total += num_of_defeated

    remainder = B[i] - num_of_defeated
    num_of_defeated = min(A[i + 1], remainder)
    A[i + 1] -= num_of_defeated
    total += num_of_defeated

print(total)

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

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

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

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

Solution (Python):

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

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

count += min(A[N], ad)     

print(count)

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

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

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

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

for i in range(n):
    tmp = min(a[i],surplus)
    kill += tmp
    a[i] -= tmp
    if b[i] > a[i]:
        kill += a[i]
        surplus = b[i]-a[i]
    else:
        kill += b[i]
        surplus = 0
tmp = min(a[n],surplus)
kill += tmp
print(kill)

Solution (Python):

n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b.append(0)
m = [0 for _ in range(n+1)]
tmp = 0

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

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

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

monster_sum = sum(A)

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

print(monster_sum - rest_sum)

Solution (Python):
N=int(input())
A=[int(x) for x in input().rstrip().split()]
B=[int(x) for x in input().rstrip().split()]
ans=0
now=A[0]
for i in range(N):
  if B[i]<=A[i]:
    ans+=B[i]
  else:
    if A[i]+A[i+1]<=B[i]:
      ans+=A[i]+A[i+1]
      A[i+1]=0
    else:
      ans+=B[i]
      A[i+1]=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()]

ans = 0

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

print(ans)

Solution (Python):
import numpy as np


n = int(input())
a = [i for i in list(map(int, input().split()))]
b = [i for i in list(map(int, input().split()))]

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

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

    for i, bb in enumerate(b):
        if bb >= a[i]:
            bb -= a[i]
            ans += a[i]
            if bb >= a[i+1]:
                ans += a[i+1]
                a[i+1] = 0
            else:
                ans += bb
                a[i+1] -= bb
        else:
            ans += bb
    print(ans)
if __name__ == '__main__':
    main()