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

Solution (Python):
n = int(input())
monster = list(map(int, input().split()))
defeatable = list(map(int, input().split()))
start = sum(monster)
for idx in range(n) :
    if defeatable[idx] <= monster[idx] :
        monster[idx] -= defeatable[idx]
        defeatable[idx] = 0
    else :
        defeatable[idx] -= monster[idx]
        monster[idx] = 0
        if defeatable[idx] <= monster[idx+1] :
            monster[idx+1] -= defeatable[idx]
            defeatable[idx] = 0
        else :
            defeatable[idx] -= monster[idx+1]
            monster[idx+1] = 0
print(start-sum(monster))

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

def main(n,a,b):
    sum=0
    for i in range(n):
        hp=b[i]
        use0=min(hp,a[i])
        a[i]-=use0
        hp-=use0
        sum+=use0
        if hp>0:
            use1=min(hp,a[i+1])
            a[i+1]-=use1
            sum+=use1
    
    return sum

if __name__=='__main__':
    n,a,b=readinput()
    ans=main(n,a,b)
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
l = []
total = sum(A)

for i in range(len(B)):
    if A[i] >= B[i]:
        t = A[i] - B[i]
        A[i] = t
    else:
        t = B[i] - A[i]
        A[i] = 0
        
        if A[i+1] >= t:
            A[i+1] = A[i+1] - t
        else:
            A[i+1] = 0
print(total - sum(A))

Solution (Python):
import sys

N = int(sys.stdin.readline())
As = [int(x) for x in sys.stdin.readline().split(" ")]
Bs = [int(x) for x in sys.stdin.readline().split(" ")]

result = 0
for i in range(0, N):
    i = (N - 1) - i
    B = Bs[i]

    right = As[i + 1]
    right_result = min(B, right)
    result += right_result
    As[i + 1] -= right_result
    B -= right_result

    left = As[i]
    left_result = min(B, left)
    result += left_result
    As[i] -= left_result
    B -= left_result

print(result)

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

ans = 0
rest = 0
for a, b in zip(B, A):
  b_ = b - rest
  if b_ > a:
    ans += rest + a
    rest = 0
  elif b_ > 0:
    rest = a - b_
    ans += b
  elif b_ <= 0:
    rest = a
    ans += b
b = A[-1]
if rest > b:
  ans += b
else:
  ans += rest
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] != 0:  
        if B[i] > A[i]:  
            B[i] -= A[i]
            count += A[i]
        else:  
            count += B[i]
            continue
    if A[i+1] < B[i]:  
        count += A[i+1]
        A[i+1] = 0
    else:  
        A[i+1] -= B[i]
        count += B[i]

print(count)

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

print(c - sum(a))

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

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

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

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

cnt=0

for i in range(n):
  taoshita=min(A[i],B[i])
  
  a=min(A[i+1],B[i]-taoshita)
  
  A[i+1]-=a
  cnt+=(taoshita+a)

print(cnt)

Solution (Python):

"""
Created on Wed Jul 15 05:49:43 2020

@author: Dell
"""

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

Solution (Python):
N=int(input())
A=list(map(int,input().strip().split()))
B=list(map(int,input().strip().split()))
"""
添字が小さい方の街からどんどん倒していけば良い。
iとi+1を倒して、B[i+1]がまだ余っている場合に、
それをi+2のモンスター倒しに繰り越して利用する。
B[i]が余っている時、その余りをi+2以降のモンスター退治に繰り越さないよう注意
"""
remain=max(0,B[0]-A[0])
mons=min(A[0],B[0])
for n in range(1,N):
    mons+=min(remain+B[n],A[n])
    remain=max(0,B[n]-max(A[n]-remain,0))
mons+=min(remain,A[N])
print(mons)

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

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

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

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

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

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

    for i in range(n-1, -1, -1):
        if blis[i] <= alis[i+1]:
            ans += blis[i]
        elif blis[i] < alis[i+1] + alis[i]:
            ans += blis[i]
            alis[i] = alis[i] - (blis[i] - alis[i+1]) 
        elif blis[i] >= alis[i+1] + alis[i]:
            ans += alis[i] + alis[i+1]
            alis[i] = 0
    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):







__giturl__ = "$URL$"


from sys import stdin
input = stdin.readline


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

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

  print(sum_)


if(__name__ == '__main__'):
  main()

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

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

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

    Z=min(A[i+1],B[i])
    A[i+1]-=Z
    S+=Z

print(S)

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)



from collections import deque

import itertools
import heapq


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    res = 0
    mons = 0
    for i in range(N):
        if B[i] + res > A[i]:
            mons += A[i]
            res = min(B[i], B[i] + res - A[i])
        else:
            mons += B[i] + res
            res = 0
    
    if res > A[N]:
        mons += A[N]
    else:
        mons += res
    print(mons)

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

Solution (Python):
n = int(input())
la = [int(w) for w in input().split()]
lb = [int(w) for w in input().split()]

ans = 0

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

    m = min(la[i + 1], lb[i])
    la[i+1] -= m
    lb[i] -= m
    ans += m

print(ans)

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


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

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


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

print(add)

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

Solution (Python):
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    n = I()
    A = LI()
    B = LI()
    ans = 0
    for i in range(n):
        b = B[i]
        a = A[i]
        A[i] = max(0, a-b)
        b = max(0, b-a)
        a = A[i+1]
        A[i+1] = max(0, a-b)
        b = max(0, b-a)
        ans += B[i]-b
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
B = [int(y) for y in input().split()]
sum = 0
for i in range(N):
    sum += min(A[i],B[i])
    sum += min(max(B[i]-A[i],0),A[i+1])
    A[i+1] += max(min(A[i]-B[i],0),-A[i+1])
print(sum)

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

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

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

  if a[i+1]<=b[i]:
    ans=ans+a[i+1]
    a[i+1],b[i]=a[i+1]-a[i+1],b[i]-a[i+1]
  else:
    ans=ans+b[i]
    a[i+1],b[i]=a[i+1]-b[i],b[i]-b[i] 
    
print(ans)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
t = 0
for i,b in enumerate(B):
    m = min(A[i], b)
    b -= m
    A[i] -= m
    t += m
    m = min(A[i+1], b)
    b -= m
    A[i+1] -= m
    t += m
print(t)

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]<=B[i]<A[i]+A[i+1]:
        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):
from sys import stdin,stdout

if __name__=="__main__":
    n=int(stdin.readline())

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

    cnt=0

    for i in range(n):
        if(a[i]>=b[i]):
            cnt+=b[i]
        else:
            cnt+=a[i]
            d=b[i]-a[i]
            cnt+=min(a[i+1],d)
            if(d>a[i+1]):
                a[i+1]=0
            else: a[i+1]-=d
            
    stdout.write(str(cnt)+"\n")

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

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

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

print(sum(A_orig)-sum(A))

Solution (Python):
f=lambda:map(int,input().split())
input()
p,*A=f()
B=f()
monster = 0
for a,b in zip(A, B):
    t = min(b, p+a)
    p = a - max(0, t-p)
    monster += t
    
print(monster)

Solution (Python):
import sys, math
from collections import defaultdict
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N = ri()
A = rl()
B = rl()

ans = 0
for i in range(N):
	for j in range(2):
		if A[i+j] >= B[i]:
			ans += B[i]
			A[i+j] -= B[i]
			B[i] = 0
		else:
			ans += A[i+j]
			B[i] = B[i] - A[i+j]
			A[i+j] = 0
print(ans)

Solution (Python):

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



if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    solve()

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

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

print(ans)

Solution (Python):
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = 0
    tail = A[N]
    idx = N-1
    while B:
        brave = B.pop()
        if brave >= tail + A[idx]:
            ans += tail + A[idx]
            tail = 0
        else:
            ans += brave
            if brave < tail:
                tail = A[idx]
            else:
                tail = A[idx] - (brave-tail)
        idx -= 1
    print(ans)



    
if '__main__' == __name__:
    resolve()

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

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

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

Solution (Python):
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    for i in range(n):
        for j in range(i, i+2):
            if a[j] >= b[i]:
                a[j] -= b[i]
                ans += b[i]
                break
            else:
                ans += a[j]
                b[i] -=  a[j]
                a[j] = 0
    print(ans)

if __name__ == '__main__':
    main()

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

ans = 0

for  i in range(N):
    tmp = 0
    if A[i] >= B[i]:
        A[i] -= B[i]
        ans += B[i]
    else:
        ans += A[i]
        tmp = B[i] - A[i]
        A[i] = 0
        if A[i+1] >= tmp:
            A[i+1] -= tmp
            ans += tmp
        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()))
total = 0
cnt = 0
total_l = []
cnt_l = []
for i in range(n):
    if a[i] - (b[i] - cnt) >= 0:
        total += b[i] - cnt
        cnt = 0
    else:
        total += a[i]
        cnt = max(-b[i], a[i] - (b[i] - cnt))

if cnt < 0:
    total += min(a[n], abs(cnt))

print(total)

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

Solution (Python):
import sys
input=sys.stdin.buffer.readline
import os
from math import*

n=int(input())
arr=list(map(int,input().split()))
brr=list(map(int,input().split()))

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

Solution (Python):

n = int(input())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
s = sum(a)
for i in range(n):
    p = a[i]
    a[i] = max(0, a[i] - b[i])
    if a[i] == 0:
        a[i + 1] = max(0, a[i + 1] + p - b[i])

print(s - sum(a))

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

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

count = 0
for i in range(N):
  if A[i] >= B[i]:
    count += B[i]
  elif A[i] < B[i]:
    count += A[i]
    B[i] -= A[i]
    A[i] = 0
    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):
import sys

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
B = list(map(int, sys.stdin.readline().split()))

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

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

if A[N] >= plus:
    ans += plus
else:
    ans += A[N]

print(ans)

Solution (Python):
n=int(input())
alist=list(map(int,input().split()))
blist=list(map(int,input().split()))
ini=sum(alist)

for i in range(n):
    tmpa=alist[i]
    tmpb=blist[i]
    if tmpb >=tmpa:
        alist[i]=0
        tmpb-=tmpa
        alist[i+1]=max(0,alist[i+1]-tmpb)
    else:
        alist[i]-=tmpb
print(ini-sum(alist))

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

Solution (Python):
n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))

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

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

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


print(cnt)

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

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

if __name__ == '__main__':
    main()

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

after = sum(A)
print(before - after)

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

for i in range(N):
  b = B[i]
  if A[i] >= b:
    A[i] -= b
  else:
    if A[i+1]+A[i] >= b:
      A[i+1] = A[i+1]+A[i]-b
      A[i] = 0
    else:
      A[i], A[i+1] = 0, 0
      
a2 = sum(A)
print(a1-a2)

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

Pre = sum(A)
for T in range(0,N):
    if A[T]<=B[T]:
        A[T+1] = max(A[T+1]-(B[T]-A[T]),0)
        A[T] = 0
    else:
        A[T] -= B[T]
Aft = sum(A)
print(Pre-Aft)

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

St = sum(A)
for T in range(0,N):
    if A[T]+A[T+1]<= B[T]:
        A[T] = 0
        A[T+1] = 0
    if A[T]<=B[T]<A[T]+A[T+1]:
        A[T+1] = A[T+1]-(B[T]-A[T])
        A[T] = 0
    if B[T]<A[T]:
        A[T] = A[T]-B[T] 
print(St-sum(A))

Solution (Python):

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

ans=0

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

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

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

print(total)

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

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

Solution (Python):
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    sumA = sum(A)
    
    for i in range(N):
        res = B[i]
        if A[i] > 0 and res != 0:
            A[i], res = max(A[i] - res, 0), max(res - A[i], 0)

        if A[i+1] > 0 and res != 0:
            A[i+1], res = max(A[i+1] - res, 0), max(res - A[i+1], 0)
    
    print(sumA - sum(A))
    return

resolve()

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

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

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

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

ans = 0
surplus = 0

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

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

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split())) 
    e = sum(B)
    for i in range(len(B)):
        attack = min(B[i],A[i])
        B[i] -= attack
        A[i] -= attack

        if i < N:
            attack = min(B[i],A[i+1])
            B[i] -= attack
            A[i+1] -= attack

    print(e-sum(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(len(a)-1):
    
    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

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

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

Solution (Python):
n = int(input())
monster = list(map(int, input().split()))
brave = list(map(int, input().split()))
res = 0
for i in range(n):
  temp = min(monster[i], brave[i])
  brave[i] -= temp
  res += temp
  temp = min(monster[i + 1], brave[i])
  monster[i + 1] -= temp
  res += temp
print(res)

Solution (Python):
from collections import defaultdict
def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

n = readInt()
a = readInts()
b = readInts()
b.append(0)
b.append(0)

s = sum(a)

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

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

    ans += min(A[i+1], B[i])
    A[i+1] = max(A[i+1] - B[i], 0)

print(ans)