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

ans_max = sum(A)

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

    else:
        if B[i-1] <= A[i]:
            A[i] -= B[i-1]
            B[i-1] = 0
        elif B[i-1] > A[i]:
            B[i-1] -= A[i]
            A[i] = 0
            
        
        if B[i] <= A[i]:
            A[i] -= B[i]
            B[i] = 0
        elif B[i] > A[i]:
            B[i] -= A[i]
            A[i] = 0

ans_min = sum(A)

ans = ans_max - ans_min

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

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

print(ans)

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

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

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

print(m)

Submission status: Accepted

Solution code:

import sys

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


def solve(N, A, B):
    B = B

    result = 0
    for i in range(0, N):
        r0 = min(A[i], B[i])
        A[i] -= r0
        B[i] -= r0
        result += r0
        debug("i=%d"%i, r0)
        if B[i] > 0:
            r = min(A[i+1], B[i])
            A[i+1] -= r
            B[i] -= r
            result += r
            debug("i=%d"%i, r)

    return result


def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]
    print(solve(N, A, B))

main()

Submission status: Accepted

Solution code:
''' بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ '''
gi = lambda : list(map(int,input().strip().split()))
n, = gi()
a = gi()
b = gi()
ans = 0
for k in range(n):
	if b[k] >= a[k]:
		ans += a[k]
		b[k] -= a[k]
	else:
		ans += b[k]
		a[k] -= b[k]
		b[k] = 0
	if b[k] > 0:
		temp = min(a[k + 1], b[k])
		a[k + 1] -= temp
		ans += temp
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
arr_base = list(map(int,input().split()))
cap_base = list(map(int,input().split()))

arr = arr_base.copy()
cap = cap_base.copy()

ans = 0
for i in range(n):
    if arr[i] < cap[i]:
        ans += arr[i]
        sup = cap[i]-arr[i]
        if sup > arr[i+1]:
            ans += arr[i+1]
            arr[i+1] = 0

        else:
            arr[i+1]-=sup
            ans += sup
    else:
        ans += cap[i]
temp = ans
ans = 0


cap = cap_base.copy()
arr= arr_base.copy()
for i in range(1,n+1):
    if arr[-i] < cap[-i]:
        ans += arr[-i]
        sup = cap[-i]-arr[-i]
        if sup > arr[-i-1]:
            ans += arr[-i-1]
            arr[-i-1] = 0
        else:
            arr[-i-1]-=sup
            ans += sup
    else:
        ans += cap[-i]
#     print(ans)
#     print(arr)
# print(temp,ans)

print(max(temp,ans))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

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

print(ans+min(A[-1],p))

Submission status: Accepted

Solution code:
N = int(input())
li = list(map(int,input().split()))	
li2 = list(map(int,input().split()))	

ans = 0
res = li[0]
for i in range (N):
        if res > li2[i]:
                ans += li2[i]
                res = li[i+1] 
        else:  
                if li[i+1] >= li2[i] - res:
                        ans += li2[i] 
                        res = li[i+1] - (li2[i] - res)
                else:
                        ans += res + li[i+1]
                        res = 0
print(ans)

Submission status: Accepted

Solution code:
N=int(input())

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


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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
ip = sys.stdin.readline

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

s = 0
for i in range(N):
    c1 = min(A[i], B[i])
    s += c1
    B[i] -= c1
    c2 = min(A[i+1], B[i])
    s += c2
    A[i+1] -= c2

print(s)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    B=LI()
    A=LI()
    ans = 0
    for i in range(N):
        ans += max(0,min(A[i],B[i]))
        A[i] -= max(0,B[i])
        ans += max(0,min(A[i],B[i+1]))
        B[i+1]-=max(0,A[i])
    #ans += max(0,min(B[-1],A[-1]))
    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()))
ans = 0
for i in range(N-1, -1, -1):
    if A[i+1] >= 1:
        kill = min(B[i], A[i+1])
        B[i] -= kill
        A[i+1] -= kill
        ans += kill
    kill = min(B[i], A[i])
    B[i] -= kill
    A[i] -= kill
    ans += kill
print(str(ans))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
r = 0
c = As[0]
for i in range(N):
  B = Bs[i]
  if B >= c+As[i+1]:
    r += c+As[i+1]
    c = 0
  elif B >= c:
    r += B
    c = As[i+1]-(B-c)
  else:
    r += B
    c = As[i+1]
print(r)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy

# If you use recursive call, uncomment this code
#sys.setrecursionlimit(10**6)

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

    ans = 0

    k = min(A[0], B[0])
    ans += k
    A[0] -= k
    B[0] -= k

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

        k = min(A[i], B[i])
        ans += k
        A[i] -= k
        B[i] -= k

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

bustered_counter = 0
for i in range(N):
    dif = A_list[i] - B_list[i]
    if dif >= 0:
        bustered_counter += B_list[i]
    elif A_list[i + 1] + dif >= 0:
        bustered_counter += B_list[i]
        A_list[i + 1] += dif
    else:
        bustered_counter += A_list[i] + A_list[i + 1]
        A_list[i + 1] = 0

print(bustered_counter)

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

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

print(total - sum(a))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
a_s = [int(item) for item in input().split()]
b_s = [int(item) for item in input().split()]
total = sum(a_s)

for i, b in enumerate(b_s):
    if b <= a_s[i]:
        a_s[i] -= b
        continue
    elif b > a_s[i]:
        a_s[i+1] = max(0, a_s[i+1] - (b - a_s[i]))
        a_s[i] = 0
    
print(total - sum(a_s))

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:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))  # N + 1
b = list(map(int, input().split()))  # N
cnt = 0

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

Submission status: Accepted

Solution code:
import math
import sys
MAX_INT = int(10e9)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = IL()
b = IL()

ans = 0
for i in range(N):
    if a[i] <= b[i]:
        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]
    else:
        ans += b[i]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num = sum(a)
# print(num)
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
aft = sum(a)
# print(aft)
print(num - aft)

Submission status: Accepted

Solution code:
#!venv/bin/python
#from collections import defaultdict 
from itertools import combinations
from collections import defaultdict
from pprint import pprint
import math
import datetime
import sys

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:


def main():
    N = int(input())
    AN = list(map(int, input().split()))
    BN = list(map(int, input().split()))

    res = 0

    for i in range(N - 1, -1, -1):
        res += min(AN[i + 1], BN[i])
        BN[i] = max(0, BN[i] - AN[i + 1])

        res += min(BN[i], AN[i])
        AN[i] = max(0, AN[i] - BN[i])

    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
city = [int(x) for x in input().split()]
hero = [int(y) for y in input().split()]
monster = 0  # 倒せるモンスターの数
for i in range(N):
    if hero[i] > city[i]:
        hero[i] -= city[i]
        monster += city[i]
        city[i] = 0
        if hero[i] > city[i + 1]:
            monster += city[i+1]
            city[i + 1] = 0
        else:
            city[i + 1] -= hero[i]
            monster += hero[i]
    else:
        monster += hero[i]


print(monster)

Submission status: Accepted

Solution code:
N = int(input())
A_list = [int(x) for x in input().split()]
B_list = [int(x) for x in input().split()]
cnt = 0
for i, B in enumerate(B_list):
    A = A_list[i]
    cnt += min(A, B)
    if (B > A):
        diff = min(A_list[i+1], B-A)
        A_list[i+1] -= diff
        cnt += diff
print(cnt)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
r = a[0]
for i in range(n):
    if b[i] <= r:
        ans += b[i]
        r = a[i + 1]
    else:
        p = b[i] - r
        if p < a[i + 1]:
            ans += b[i]
            r = a[i + 1] - p
        else:
            ans += r + a[i + 1]
            r = 0
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]:
        ans += B[i]
    else:
        ans += A[i]
        B2 = B[i] - A[i]
        if A[i+1] >= B2:
            ans += B2
            A[i+1] -= B2
        else:
            ans += A[i+1]
            A[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()))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = [0] * (N+3)
B.append(0)
B.append(0)
total = 0
for i in range(N+1):
    if(A[i] <= B[i] + C[i]):
        total += A[i]
        if(A[i] <= C[i]):
            C[i+1] = B[i]
        else:
            C[i+1] = B[i] + C[i] - A[i]
    else:
        total += B[i] + C[i]



print(total)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, d=DVSR): return pow(x, d - 2, d)
def DIV(x, y, d=DVSR): return (x * INV(y, d)) % d
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
N=II()
AS=LI()
BS=LI()

sm = 0
for i in range(N):
    rest = BS[i]
    taosu = min(AS[i], rest)
    AS[i] -= taosu
    rest -= taosu
    sm += taosu

    taosu2 = min(AS[i+1], rest)
    AS[i+1] -= taosu2
    rest -= taosu2
    sm += taosu2

print(sm)

Submission status: Accepted

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

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

print(score)

Submission status: Accepted

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

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

print(z)

Submission status: Accepted

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

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

  if limit > A[i+1]:
    answer += A[i+1]
    limit -= A[i+1]
    A[i+1] = 0
  else:
    A[i+1] -= limit
    answer += limit
    limit = 0

print(answer)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder A
"""

import sys, math, random
import numpy as np

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column
# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']
# S.replace('ABC','X') # "testABCABC" -> "testXX"

inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    N=I()
    A = list(map(int,input().split()))
    B=LI()
    bef=0
    out=0
    for i in range(N):
        if A[i]>bef:
            en=A[i]-bef
            out+=bef
            if en>B[i]:
                bef=0
                out+=B[i]
            else:
                bef=B[i]-en
                out+=en
        else:
            out+=A[i]
            bef=B[i]
    i=N
    if A[N]>bef:
        en=A[N]-bef
        out+=bef
    else:
        out+=A[N]
        # bef=B[N]

    print(out)

if __name__ == "__main__":
    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 range(n):
  att = min(a[i], b[i])
  a[i] -= att
  b[i] -= att
  ans += att
  att = min(a[i + 1], b[i])
  a[i + 1] -= att
  b[i] -= att
  ans += att
print(ans)

Submission status: Accepted

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

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

print(mon)

Submission status: Accepted

Solution code:
import sys
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
sys.setrecursionlimit(int(1e+6))
MOD = int(1e+9) + 7
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3

def main():
    N = i2n()
    A = i2nn()
    B = i2nn()
    n = 0   # 倒すモンスター数
    for i in range(N):
        t = min(A[i], B[i])
        n += t
        A[i] -= t
        B[i] -= t
        t = min(A[i+1], B[i])
        n += t
        A[i+1] -= t
        B[i] -= t
    print(n)
    #print(A)
    #print(B)

main()

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

def main(): 
    N = II()
    A = LI()
    B = LI()

    A_sum = sum(A)  # モンスターの合計数

    # 貪欲

    for i in range(N):
        # まずは自分と同じ番号の町を見る。
        if A[i] >= B[i]: # 街にいるモンスターのほうが多いとき
            A[i] -= B[i]  # 倒せるだけ倒す。
            B[i] = 0
        else: # 勇者の能力のほうが高いとき
            B[i] -= A[i]
            A[i] = 0

        # 次に次の街の面倒を見る。
        if A[i+1] >= B[i]:
            A[i+1] -= B[i]  # 倒せるだけ倒す。
            B[i] = 0
        else: # 勇者の能力のほうが高いとき
            B[i] -= A[i+1]
            A[i+1] = 0

    print(A_sum - sum(A))


main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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]
    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] = 0
        A[i+1] = 0
print(ans)

Submission status: Accepted

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

s = 0
for i in range(N):
    if Bs[i] > As[i]:
        s += As[i]
        Bs[i] -= As[i]
    else:
        s += Bs[i]
        Bs[i] = 0
    if Bs[i] > As[i + 1]:
        s += As[i + 1]
        As[i + 1] = 0
    else:
        s += Bs[i]
        As[i + 1] -= Bs[i]
print(s)