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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
count = 0 
for i in range(n):
  count += min(a[i] + a[i + 1], b[i])
  if a[i] < b[i]:
    a[i + 1] = max(a[i + 1] - (b[i] - a[i]), 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):
    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):
from math import factorial as fact
import math
import sys
import itertools
import numpy as np
from collections import Counter
import datetime
from collections import deque



def input1():
	return int(input())


def input2():
	return map(int,input().split())


def input_array():
	return list(map(int,input().split()))

def keta(N):
	return len(str(N))


def input_daytime(input_time):
	time,distance=input_time.split()
	t=time.split(":")
	se=str(t[-1]).split(".")
	dt1 = datetime.timedelta(hours=int(t[0]),minutes=int(t[1]),seconds=int(se[0]),milliseconds=int(se[1]))
	return dt1

def combinations(n,r):
	return list(itertools.combinations(range(n),r))



def all_sets(num_list):
	subsets=[]
	for i in range(2,len(num_list) + 1):
		for c in combinations(num_list, i):
			subsets.append(c) 
	return subsets


def ch(x1):
	cf=[]
	for i in range(2,math.sqrt(x1)+1):
		if x1 % i==0:
			cf.append(i)
	return cf

def CountOneRoots(x,y):
	
	return fact(x+y)/fact(x)/fact(y)


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
	
def count_str(m):
	tmp={}
	for i in range(10):
		if i in tmp:
			tmp[i]=1
		else:
			tmp[i]+=1
	return tmp

def is_prime(n):
	if n==1: return True

	for i in range(2,int(math.sqrt(n))+1):
		if n%i==0:
			return False
	return True

n=input1()
A=input_array()
B=input_array()
next_num=0 
count=0 

for i in range(n+1):
	if A[i]<=next_num:
		count+=A[i]
		A[i]=0
	else:
		A[i]-=next_num
		count+=next_num
	if i==n:
		break
	if A[i]<B[i]:
		count+=A[i]
		next_num=B[i]-A[i]
	else:
		count+=B[i]
		next_num=0

print(count)

Solution (Python):


import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))

from itertools import accumulate
def resolve():
    N,=pin()
    A=lispin()
    B=lispin()
    cnt=0
    for i in range(N):
        if A[i]>B[i]:
            cnt+=B[i]
        else:
            t=B[i]-A[i]
            x=A[i+1]-t
            if x<0:
                cnt+=A[i]+A[i+1]
                A[i+1]=0
            else:
                A[i+1]=x
                cnt+=B[i]
    print(cnt)
        
    

resolve()

Solution (Python):
import sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def I(): return int(sys.stdin.readline().rstrip())
N = I()
A = LI()[::-1]
B = LI()[::-1]
ans = 0
for i in range(N):
    b = B[i]
    a = A[i]
    ans += min(a,b)
    b -= a
    if b<=0:
        continue
    a2 = A[i+1]
    ans += min(a2,b)
    A[i+1] = a2-(min(a2,b))
print(ans)

Solution (Python):


N = int(input())
A = [int(i) for i in (input().split())]
B = [int(i) for i in (input().split())]
cnt = 0 
for i in range(len(A)-1):

    cnt += min(A[i]+A[i+1],B[i])
    if A[i] < B[i]:
        A[i+1] = max(A[i+1] - (B[i] - A[i]),0)
print(cnt)

Solution (Python):


N = int(input())
A = [int(i) for i in (input().split())]
B = [int(i) for i in (input().split())]
cnt = 0 
for i in range(len(A)-1):

    cnt += min(A[i]+A[i+1],B[i])
    if A[i] < B[i]:
        A[i+1] = max(A[i+1] - (B[i] - A[i]),0)
print(cnt)

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

remained_monsters = [int(s) for s in input().split()]
remained_capacities = [int(s) for s in input().split()]
n_killed = 0

for i in range(n):
    if remained_monsters[i] <= remained_capacities[i]:
        n_killed += remained_monsters[i]
        remained_capacities[i] -= remained_monsters[i]
        remained_monsters[i] = 0
    else:
        n_killed += remained_capacities[i]
        remained_monsters[i] -= remained_capacities[i]
        remained_capacities[i] = 0
    if remained_monsters[i + 1] <= remained_capacities[i]:
        n_killed += remained_monsters[i + 1]
        remained_capacities[i] -= remained_monsters[i + 1]
        remained_monsters[i + 1] = 0
    else:
        n_killed += remained_capacities[i]
        remained_monsters[i + 1] -= remained_capacities[i]
        remained_capacities[i] = 0

print(n_killed)

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

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

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

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

Ans1=0
Ans1=sum(A)

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

Ans2=0
Ans2=sum(A)        
        
print(Ans1-Ans2)

Solution (Python):
def main():
    N=int(input())
    A=[int(_) for _ in input().split()]
    B=[int(_) for _ in input().split()]
    ans=0
    for i in range(N):
        if A[i]<B[i]:
            
            ans += (A[i]+min(A[i+1],B[i]-A[i]))
            A[i+1] = max(A[i+1]-(B[i]-A[i]),0)
        else:
            
            ans += min(A[i],B[i])
    print(ans)

main()

Solution (Python):

import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
B.append(0)
count =0
residue_attack_possible =[0]*(N+2)
residue_monster =0
for i in range(N+1):
    if A[i] <= B[i] +residue_attack_possible[i]:
        count +=A[i]
        residue_attack_possible[i+1] = B[i] -max(0,A[i] -residue_attack_possible[i])
    else:
        count +=B[i] +residue_attack_possible[i]
        

print(count)

Solution (Python):
N = int(input())
arr_a = list(map(int, input().split()))
arr_b = list(map(int, input().split()))

ans = 0
for i in range(len(arr_b)):
    if arr_b[i] >= arr_a[i]+arr_a[i+1]:
        ans += arr_a[i]+arr_a[i+1]
        arr_a[i] = 0
        arr_a[i+1] = 0

    elif arr_b[i] < arr_a[i]+arr_a[i+1]:
        if arr_b[i] <= arr_a[i]:
            ans += arr_b[i]
            arr_a[i] -= arr_b[i]

        elif arr_b[i] > arr_a[i]:
            ans += arr_b[i]
            arr_a[i+1] -= arr_b[i]
            arr_a[i+1] += arr_a[i]
            arr_a[i] = 0
print(ans)

Solution (Python):
n = int(input())
a_ls = list(map(int, input().split()))
b_ls = list(map(int, input().split()))

ret = 0

for i in range(n):
    if a_ls[i] >= b_ls[i]:
        ret += b_ls[i]
    else:
        ret += a_ls[i]
        left = b_ls[i] - a_ls[i]
        ret += min(left, a_ls[i+1])
        a_ls[i+1] = max(0, a_ls[i+1] - left)

print(ret)

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

Solution (Python):
n = int(input())
a_input = list(map(int,input().split()))
b_input = list(map(int,input().split()))

ans=0
for i in range(n):
    first_attack=0
    second_attack=0

    first_attack = min(b_input[i],a_input[i])
    a_input[i]-=first_attack
    if first_attack<b_input[i]:
        second_attack = min(a_input[i+1],b_input[i]-first_attack)
        a_input[i+1]-= second_attack
    ans+=first_attack+second_attack

print(ans)

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

if B[n-1]<A[n-1]+A[n]:
    
    sum+=B[n-1]
else:
    
    sum+=A[n-1]+A[n]
print(sum)

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

if B[n-1]<A[n-1]+A[n]:
    
    sum+=B[n-1]
else:
    
    sum+=A[n-1]+A[n]
print(sum)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce


import sys
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


n = I()
A = LI()
B = LI()

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

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

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

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

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


solve()

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

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

sa = sum(A)

for i, b in enumerate(B):
    tmp = min(A[i], b)  
    A[i] -= tmp  
    A[i + 1] -= min(A[i + 1], b - tmp)  

print(sa - sum(A))

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


c = []
count = 0

for i in range(n):

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

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

        cnt += A[i] + (B[i]-A[i]) 

    elif B[i] < A[i] + A[i+1] and B[i] < A[i]:
        cnt +=B[i]

print(cnt)

Solution (Python):


N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]

total = 0
for i in range(N):
    attack = min(A[i], B[i])
    A[i] -= attack
    B[i] -= attack
    total += attack
    if 0 < B[i]:
        attack2 = min(B[i], A[i + 1])
        A[i + 1] -= attack2
        B[i] -= attack2
        total += attack2

print(total)

Solution (Python):
n = int(input())
alis = list(map(int, input().split()))
blis = list(map(int, input().split()))
ans = 0
cross = 0
for i in range(n):
    front = min(alis[i]-cross, blis[i])
    cross = min(alis[i+1], blis[i]-front)
    ans += front + cross
print(ans)

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

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

print(count)

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

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

C=[]
for i in range(N):
    C.append(min(A[i+1]+A[i],B[i]))
    if B[i]>A[i]:
        A[i+1]=A[i+1]-(B[i]-A[i])
        if A[i+1]<0:
            A[i+1]=0

print(sum(C))

Solution (Python):
import sys

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

res = 0
for i, b in enumerate(B):
    a = A[i]
    a1 = A[i + 1]
    if a >= b:
        res += b
    else:
        res += a
        if a1 >= b - a:
            A[i + 1] = a1 - (b - a)
            res += b - a
        else:
            A[i + 1] = 0
            res += a1

print(res)

Solution (Python):
import sys, heapq, bisect, math, fractions
from collections import deque

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

res = 0
for i, b in enumerate(B):
    a = A[i]
    a1 = A[i + 1]
    if a >= b:
        res += b
    else:
        res += a
        if a1 >= b - a:
            A[i + 1] = a1 - (b - a)
            res += b - a
        else:
            A[i + 1] = 0
            res += a1

print(res)

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

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

ans = 0
for i in range(n):
    town_a = a[i]
    town_b = a[i+1]
    power = b[i]
    if power > town_a:
        ans += town_a
        power -= town_a
    else:
        ans += power
        continue
    if power > town_b:
        ans += town_b
        a[i+1] = 0
    else:
        ans += power
        a[i+1] -= power
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(0,n):
  attack=b[i]
  if attack>=a[i]:
    count+=a[i]
    attack-=a[i]
    a[i]=0
  else:
    count+=attack
    a[i]-=attack
    attack=0
  if attack>=0:
    if attack>=a[i+1]:
      count+=a[i+1]
      attack-=a[i+1]
      a[i+1]=0
    else:
      count+=attack
      a[i+1]-=attack
      attack=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):
    town_a = a[i]
    town_b = a[i+1]
    power = b[i]
    if power > town_a:
        ans += town_a
        power -= town_a
    else:
        ans += power
        continue
    if power > town_b:
        ans += town_b
        a[i+1] = 0
    else:
        ans += power
        a[i+1] -= power
print(ans)

Solution (Python):
def main():
    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):
        k = B[i] - A[i]
        if k >= 0:
            if k <= A[i + 1]:
                ans += B[i]
                A[i + 1] -= k
            else:
                ans += A[i] + A[i + 1]
                A[i + 1] = 0
        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()))
m = sum(a)
for i in range(n):
    if a[i] <= b[i]:
        b[i] -= a[i]
        a[i] = 0
    else:
        a[i] -= b[i]
        b[i] = 0
    if a[i+1] <= b[i]:
        b[i] -= a[i]
        a[i+1] = 0
    else:
        a[i+1] -= b[i]
        b[i] -= a[i+1]
print(m - sum(a))

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

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

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

Solution (Python):
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))

    ans = 0
    for i, b in enumerate(bb):
        a = aa[i]
        next_a = aa[i+1]

        if a < b:
            ans += a
            remain = b - a

            if remain <= next_a:
                ans += remain
                aa[i+1] -= remain
            else:
                ans += next_a
                aa[i+1] = 0
        elif a >= b:
            ans += b

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """2
3 5 2
4 5"""
        output = """9"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """3
5 6 3 8
5 100 8"""
        output = """22"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """2
100 1 1
1 100"""
        output = """3"""
        self.assertIO(input, output)

if __name__ == "__main__":
    
    resolve()

Solution (Python):
def resolve():
    n = int(input())
    monster = [int(i) for i in input().split()]
    damage = [int(i) for i in input().split()]

    before = sum(monster)
    for i, (mst, dmg) in enumerate(zip(monster, damage)):
        if dmg >= monster[i]:
            _dmg = dmg - monster[i]
            monster[i] = 0
            monster[i + 1] = max(monster[i + 1] - _dmg, 0)
        else:
            monster[i] = monster[i] - dmg

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


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

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

sumA = sum(As)
for i in range(N):
    num = min(As[i], Bs[i])
    As[i] -= num
    Bs[i] -= num
    num = min(As[i+1], Bs[i])
    As[i+1] -= num
    Bs[i] -= num

ans = sumA - sum(As)
print(ans)

Solution (Python):
N = int(input())
A = [int(s) for s in input().split()]
sum_A = sum(A)
B = [int(s) for s in input().split()]
for i in range(N):
    attack1 = A[i] - B[i]
    if attack1 < 0:
        attack2 = A[i + 1] + attack1
        A[i] = 0
        A[i + 1] = attack2 if attack2 >= 0 else 0
    else:
        A[i] = attack1

print(sum_A - sum(A))

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split(' ')))
    B = list(map(int, input().split(' ')))
    result = 0
    dead_enemy = min([A[0], B[0]])  
    result += dead_enemy
    rest_power = B[0] - dead_enemy
    for i in range(1, N + 1):
        
        dead_enemy = min([A[i], rest_power])
        result += dead_enemy
        rest_enemy = A[i] - dead_enemy
        
        if i <= N - 1:
            dead_enemy = min([rest_enemy, B[i]])
            result += dead_enemy
            rest_power = B[i] - dead_enemy
    print(result)


if __name__ == '__main__':
    main()

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

for i in range(n):
    for j in range(i, i+2):
        t = a[j] - b[i]
        if t >= 0:
            a[j] = t
            b[i] = 0
        else:
            a[j] = 0
            b[i] = abs(t)

print(sb if sb - sum(b) == 0 else sb - sum(b))

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

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

print(cnt)

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

Solution (Python):
N = int(input())
la = list(map(int, input().split()))
lb = list(map(int, input().split()))

mon = 0
for i in range(N):
    if lb[i] <= la[i]:
        mon += lb[i]
    elif lb[i] > la[i]:
        if lb[i] - la[i] >= la[i+1]:
            mon += la[i] + la[i+1]
            la[i+1] = 0
        else:
            mon += la[i] + lb[i] - la[i]
            la[i+1] -= lb[i] - la[i]

print(mon)

Solution (Python):
from sys import stdin
import math
import fractions

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

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

print(cnt)

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

fii=min(a[0],b[0])
fij=min(a[1],b[0]-fii)
icnt=fii+fij
for i in range(1,n):
    fii=min(a[i]-fij,b[i])
    fij=min(a[i+1],b[i]-fii)
    icnt+=fij+fii

print(icnt)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

c = [0] * n

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

print(sum(c))

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

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

print(ans)

Solution (Python):
ii = lambda:int(input())
mi = lambda:list(map(int,input().split()))
ix = lambda x:list(input() for _ in range(x))
mix = lambda x:list(li() for _ in range(x))
iix = lambda x:list(int(input()) for _ in range(x))

def main(n,a,b):
    before = sum(a)
    for i in range(len(b)):
        if b[i] < a[i]:
            a[i] -= b[i]
        else:
            a[i+1] = max(0, a[i] + a[i+1] - b[i])
            a[i] = 0
    return before - sum(a)
            
n = ii()
a = mi()
b = mi()
print(main(n,a,b))

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

Solution (Python):
n = int(input())
mons = list(map(int,input().split()))
yusya = list(map(int,input().split()))
mons[::-1]
yusya[::-1]

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

print(ans)

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

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

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

        if a[i + 1] > b[i]:
            result += b[i]
            a[i + 1] -= b[i]
            b[i] = 0
        else:
            result += a[i + 1]
            b[i] -= a[i + 1]
            a[i + 1] = 0
    
print(result)

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

Solution (Python):
N = int(input())
arra = list(map(int,input().split()))
arrb = list(map(int,input().split()))

hero_res = 0
def_cnt = 0
for i in range(0,N+1):
    nmons = arra[i]
    mons_res = max(nmons - hero_res, 0)
    def_cnt += nmons - mons_res

    if i < N:
        hero = arrb[i]
        mons_res_2 = max(mons_res - hero, 0)
        def_cnt += mons_res - mons_res_2
        hero_res = hero - (mons_res - mons_res_2)
print(def_cnt)

Solution (Python):
import sys
input = sys.stdin.readline
import math
def main():
   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:
           CNT += A[i]
           B[i] -= 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)

if __name__ == '__main__':
    main()