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



Sample input:
2
3 5 2
4 5


Sample output:
9

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
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],b[i])
  if a[i] > b[i]:
    continue
  
  tmp = b[i] - a[i]
  count += min(a[i+1], tmp)
  a[i+1] = max(0, a[i+1]-tmp)
  
  
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
#左側から倒せるだけ倒すのが正解
Monst = 0
killed = 0
for n,b in enumerate(B):
        cur = A[n]
        nex = A[n+1]
        cur -= killed
        if b<=cur:
            Monst += b
            killed = 0
        elif b<=cur+nex:
            Monst += b
            killed = b - cur
        else:
            Monst += cur+nex
            killed = nex
print(Monst)

Submission status: Accepted

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

cnt = 0
res = 0
for i in range(N):
    enm = min(A[i]+A[i+1]-res, B[i])
    res = max(res + enm - A[i], 0)
    cnt += enm

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))

ans=0
for i in range(n-1,-1,-1):
	val=min(arr2[i],arr1[i+1])
	ans+=val
	arr2[i]-=val
	arr1[i+1]-=val
	val=min(arr2[i],arr1[i])
	ans+=val
	arr2[i]-=val
	arr1[i]-=val
	#print(arr1,arr2)
print(ans)

Submission status: Accepted

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

cnt=0
for n in range(N):
    mons = min(A[n],B[n])
    A[n] -= mons
    B[n] -= mons
    cnt += mons
    if B[n]>0:
        mons = min(A[n+1], B[n])
        A[n+1] -= mons
        cnt += mons

print(cnt)

Submission status: Accepted

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

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right; from collections import Counter, defaultdict, deque, OrderedDict; from copy import deepcopy; from fractions import gcd; from functools import lru_cache, reduce; from math import ceil, floor; from sys import stdin, setrecursionlimit
import heapq as hq; import itertools as its; import operator as op


# globals
inf = float('inf')
input = stdin.readline


def main():
    n = get_int()
    A = get_li()
    B = get_li()
    ans = 0
    left = 0
    for i, a in reversed(list(enumerate(A))):
        if i == 0:
            ans += min(left, A[0])
            break
        a_ = max(0, a - left)
        ans += a - a_
        left = max(0, B[i-1] - a_)
        ans += B[i-1] - left
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def gets(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input().strip()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().strip().split(sep=sep))]
        for _ in range(n)
    )))


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

Submission status: Accepted

Solution code:
from copy import *
from sys import *
import math
import queue
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)

def main():
	n = int(input())
	a = list(map(int,input().split()))
	b = list(map(int,input().split()))
	t = sum(b)
	for i in reversed(range(n)):
		c = a[i+1] - b[i]
		a[i+1] = 0 if c<0 else c
		b[i] = 0 - c if c < 0 else 0
		c = a[i] - b[i]
		a[i] = 0 if c<0 else c
		b[i] = 0 - c if c < 0 else 0
	print(t-sum(b))

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

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

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(m,n):
	x = max(m,n)
	y = min(m,n)
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	return y

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    a,b = LI()
    if (a-b)%2 == 0:
        print((a+b)//2)
    else:
        print("IMPOSSIBLE")
    return

#B
def B():
    n = I()
    p = LI()
    k = [p[i] for i in range(n)]
    k.sort()
    s = 0
    for i in range(n):
        s += (p[i] != k[i])
    if s < 3:
        print("YES")
    else:
        print("NO")
    return

#C
def C():
    n = I()
    a = LI()
    b = LI()
    b.append(0)
    ans = 0
    sb = sum(b)
    i = n
    while i >= 0:
        if a[i] > b[i]:
            a[i] -= b[i]
            b[i] = 0
        else:
            b[i] -= a[i]
            a[i] = 0
        if a[i] > b[i-1]:
            a[i] -= b[i-1]
            b[i-1] = 0
        else:
            b[i-1] -= a[i]
            a[i] = 0
        i -= 1

    print(sb-sum(b))
    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline

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

ans=0
for i,b in enumerate(bb):
    if b<=aa[i]:
        ans+=b
    else:
        ans+=aa[i]
        b-=aa[i]
        if b<=aa[i+1]:
            aa[i+1]-=b
            ans+=b
        else:
            ans+=aa[i+1]
            aa[i+1]=0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

a = list(map(int, input().split()))
b = list(map(int, input().split())) + [0]
c = [0] * (n + 1)

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

print(cnt)

Submission status: Accepted

Solution code:
# ABC135
# greedy algorithm
def main():
    n = int(input())
    A = list(map(int, input().rstrip().split()))
    B = list(map(int, input().rstrip().split()))
    ans = 0
    for i in range(n):
        cur1 = min(A[i], B[i])
        cur2 = min(A[i + 1], B[i] - cur1)
        A[i] -= cur1
        A[i + 1] -= cur2
        ans += cur1 + cur2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(n):
    ans += min(a[i] + a[i + 1], b[i])
    r = max(0, b[i] - a[i])
    a[i + 1] = max(0, a[i + 1] - r)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = B[:]
output = 0

for i in range(N):
    C[i],A[i] = max(C[i]-A[i],0),max(A[i]-C[i],0)
    C[i],A[i+1] = max(C[i]-A[i+1],0),max(A[i+1]-C[i],0)

for i in range(N):
    output += B[i]-C[i]

print(output)

Submission status: Accepted

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

ans = 0
pre = 0
for a, b in zip(A, B):
    if a >= pre:
        a -= pre
        ans += pre
    else:
        ans += a
        pre = b
        continue
    if a >= b:
        ans += b
        pre = 0
    else:
        ans += a
        pre = b - a
a = A[-1]
if a >= pre:
    ans += pre
else:
    ans += a
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

num = sum(a)

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

print(num -sum(a))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

def ri():
    return int(input())

def rl():
    return list(input().split())

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

def main():
    n = ri()
    la = rli()
    lb = rli()
    ans = 0
    for i in range(len(lb)):
        b = lb[i]
        a0 = la[i]
        a1 = la[i+1]
        d0 = min(b, a0)
        b -= d0
        d1 = min(b, a1)
        la[i+1] = a1-d1
        ans += d0 + d1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
from functools import reduce
import sys
import math
import bisect
import random
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return list(map(float, sys.stdin.readline().split()))
def LS():return sys.stdin.readline().split()
def SR(n): return [list(sys.stdin.readline())[:-1] for i in range(n)]
def IR(n): return [int(sys.stdin.readline()) for i in range(n)]
def LIR(n): return [list(map(int, sys.stdin.readline().split())) for i in range(n)]
def LSR(n): return [list(map(list, sys.stdin.readline().split())) for i in range(n)]
_gcd = lambda x, y: _gcd(y, x%y) if (x%y) else y
_lcm = lambda x, y: x*y // _gcd(x, y)
def gcd(*numbers):
    return reduce(_gcd, numbers)
def lcm(*numbers):
    return reduce(_lcm, numbers)
sys.setrecursionlimit(1000000)
INF = float('inf')
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

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

Submission status: Accepted

Solution code:
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]:
        t=b[i]
        a[i] -= t
        b[i] -= t
        ans += t
    else:
        t=a[i]
        a[i] -= t
        b[i] -= t
        ans += t

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

Submission status: Accepted

Solution code:
def get_ints():
    return list(map(int, input().split()))

n = int(input())
a = get_ints()
b = get_ints()

total = 0

for i in range(n):
    l = min(b[i], a[i]);
    total += l;
    if b[i] > l:
        r = min(b[i] - l, a[i + 1])
        total += r
        a[i + 1] -= r

print(total)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]", B: "List[int]"):
    counter = 0
    for i in range(N):
        if A[i] >= B[i]:
            counter += B[i]
            A[i] -= B[i]
            B[i] = 0
        else:
            counter += A[i]
            B[i] -= A[i]
            A[i] = 0
            if A[i+1] >= B[i]:
                counter += B[i]
                A[i+1] -= B[i]
                B[i] = 0
            else:
                counter += A[i+1]
                B[i] -= A[i+1]
                A[i+1] = 0
    counter += min(A[N-1] + A[N], B[N-1])
    print(counter)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N+1) ]  # type: "List[int]"
    B = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A, B)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l(): #intのlist
	return list(map(int,input().split()))
def m(): #複数文字
	return map(int,input().split())
def onem(): #Nとかの取得
	return int(input())
def s(x): #圧縮
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x): #listをスペースごとに分ける
	return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
	return max(map(max,x))

n = onem()

a = l()

b = l()

co = 0

if a[0] <= b[0]:
	co += a[0]
	nokori = b[0] - a[0]
	a[0] = 0
else:
	nokori = 0
	co = b[0]


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

if nokori >= a[-1]:
	co += a[-1]
else:
	co += nokori

print(co)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict

# from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))

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

R1 = [0] * N
R2 = [0] * N
for i in range(N - 1, -1, -1):
    R1[i] = min(A[i + 1], B[i])
    B[i] -= R1[i]
    R2[i] = min(A[i], B[i])
    A[i] -= R2[i]

ans = sum(R1) + sum(R2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=list(map(int, input().split()))
N=N[0]
A=list(map(int, input().split()))
B=list(map(int, input().split()))
B.append(0)
kill=0

temp=min(A[0],B[0])
kill+=temp
A[0]-=temp
B[0]-=temp

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

print(kill)

Submission status: Accepted

Solution code:
N = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
count = 0
for i in range(N):
	m1 = min(a_list[i], b_list[i])
	count += m1
	b_list[i] -= m1
	m2 = min(a_list[i + 1], b_list[i])
	count += m2
	a_list[i + 1] -= m2
print(count)

Submission status: Accepted

Solution code:
n = int(input())
alst = [int(v) for v in input().split()]
blst = [int(v) for v in input().split()]

buster = 0
rage = 0
for a, b in zip(alst, blst):
    buster += min(a, rage)
    a -= min(a, rage)
    buster += min(a, b)
    if a < b:
        rage = b - a
    else:
        rage = 0
buster += min(alst[-1], rage)

print(buster)

Submission status: Accepted

Solution code:
def compute_max(n, xs, ys):
    rest_enemys = xs[:]
    rest_powers = ys[:]
    for i in range(n):
        # city i
        removed_enemy = min(rest_enemys[i], rest_powers[i])
        rest_enemys[i] -= removed_enemy
        rest_powers[i] -= removed_enemy
        # city i + 1
        removed_enemy = min(rest_enemys[i + 1], rest_powers[i])
        rest_enemys[i + 1] -= removed_enemy
        rest_powers[i] -= removed_enemy
    total = sum(xs) - sum(rest_enemys)
    return total


def main():
    n = int(input())
    xs = [int(x) for x in input().split()]
    ys = [int(y) for y in input().split()]
    res = compute_max(n, xs, ys)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B):
    S = sum(B)


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

        ab = min(A[i], B[i])
        A[i] -= ab
        B[i] -= ab
    ab = min(A[N], B[N-1])
    A[N] -= ab
    B[N-1] -= ab
    
    return S - sum(B)


def main():
    N = read_int()
    A = read_int_n()
    B = read_int_n()
    print(slv(N, A, B))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  alist = list(map(int, input().split()))
  blist = list(map(int, input().split()))
  ans = 0
  for i, b in enumerate(blist):
    if alist[i] < b:
      b -= alist[i]
      ans += alist[i]
      alist[i] = 0
    else:
      ans += b
      b = 0
    if alist[i+1] < b:
      ans += alist[i+1]
      alist[i+1] = 0
    else:
      ans += b
      alist[i+1] -= b
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split())) + [0]

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True
#debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    N = II()
    a_list = LI()
    b_list = LI()

    ans = 0
    left = a_list[0]

    for i in range(N):
        power = b_list[i]
        if power >= left:
            # 左をすべて倒せる
            ans += left
            power = power - left
        else:
            # 左をすべて倒せない
            ans += power
            power = 0

        right = a_list[i+1]
        if power >= right:
            # 右をすべて倒せる
            ans += right
            left = 0 # 次の勇者のleftは0
        else:
            # 右が余るとき
            ans += power
            left = right - power # 次の勇者にはあまりがある

    print(ans)
solve()

Submission status: Accepted

Solution code:

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]

    else:

        B[i] = B[i] - A[i]
        ans += A[i]

        if A[i+1] >= B[i]:

            A[i+1] = A[i+1] - B[i]
            ans += B[i]

        else:

            B[i] = B[i] - A[i+1]
            ans += A[i+1]
            A[i+1] = 0

print (ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    monsters = list(map(int,input().split()))
    heroes = list(map(int,input().split()))
    total = 0
    
    for i in range(n):
        if heroes[i] >= monsters[i]:
            total += monsters[i]
            heroes[i] -= monsters[i]
        else:
            total += heroes[i]
            heroes[i] = 0

        if heroes[i] >= monsters[i+1]:
            total += monsters[i+1]
            heroes[i] -= monsters[i+1]
            monsters[i+1] = 0
        else:
            total += heroes[i]
            monsters[i+1] -= heroes[i]
            heroes[i] = 0

    print(total)

main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]", B: "List[int]"):
    c = 0
    for i in range(N):
        b, a = B[i], A[i]
        if b >= a:
            c += a
            b -= a
            if A[i+1] >= b:
                c += b
                A[i+1] -= b
            else:
                c += A[i+1]
                A[i+1] = 0
        else:
            c += B[i]
    
    print(c)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N+1) ]  # type: "List[int]"
    B = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A, B)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

init = sum(A)
remain = 0
for i in range(N):
    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(init - sum(A))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
left = 0
score = 0
for i in reversed(range(N+1)):
    if i == 0:
        score += min(left, A[i])
        continue
    monster = A[i]
    if left >= A[i]:
        score += A[i]
        left = B[i-1]
    else:
        score += left
        monster = A[i]-left
        if B[i-1] >= monster:
            score += monster
            left = B[i-1]-monster
        else:
            score += B[i-1]
            left = 0
print(score)

Submission status: Accepted

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


killed = 0
for i in range(n):
    mn = min(B[i], A[i])
    killed += mn
    A[i] -= mn
    B[i] -= mn
    mn = min(B[i], A[i+1])
    killed += mn
    A[i+1] -= mn
    B[i] -= mn

print(killed)