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

count = 0

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

print(count)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

curr = min(A[0], B[0])
ans = min(A[0], B[0])

B[0] -= ans
  
for i in range(N):
  #B[i]がA[i+1]を残った力で攻撃
  curr = min(A[i+1], B[i])
  ans += curr
  A[i+1] -= curr
  #B[i+1]がA[i+1]を攻撃
  curr = min(A[i+1], B[i+1])
  ans += curr
  B[i+1] -= curr
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
Monster = list(map(int, input().split()))
Yusha = list(map(int, input().split()))

total = 0

for i in range(N+1):
    if i > 0:
        Min = min(Monster[i], Yusha[i-1])
        Monster[i] -= Min
        Yusha[i-1] -= Min
        total += Min

    if i < N:
        Min = min(Monster[i], Yusha[i])
        Monster[i] -= Min
        Yusha[i] -= Min
        total += Min

print(total)

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n): return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n=n0()
a=n1()
b=n1()
ans=0

for i in range(n):
    a1,a2,b1=a[i],a[i+1],b[i]
#     print(a,b,ans)
    if a1-b1>=0:
        ans+=b1
    else:
        ans+=a1
        b1-=a1
        a[i+1]-=min(b1,a2)
        ans+=min(b1,a2)
# print(a,b,ans)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
total_before = 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
        
    A[i+1] = max(0, A[i+1] - B[i])

total_after = sum(A)
print(total_before - total_after)

Submission status: Accepted

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


def main():
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    ans = 0
    for i in range(N):
        if As[i] >= Bs[i]:
            ans += Bs[i]
        else:
            ans += As[i]
            ans += min(Bs[i] - As[i], As[i+1])
            As[i+1] -= Bs[i] - As[i]
            As[i+1] = max(0, As[i+1])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

p = 0
ans = 0
for enemy, power in zip(A, B):
    if enemy >= p:
        enemy -= p
        ans += p
    else:
        ans += enemy
        enemy = 0
        p = 0
    if enemy <= power:
        p = power - enemy
        ans += enemy
    else:
        ans += power
        p = 0
ans += min(p, 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):
  t = 0
  if b[i] <= a[i]:
    ans += b[i]
    a[i] -= b[i]
  else:
    t = a[i] + a[i+1]
    if b[i] > t:
      a[i+1] = 0
      ans += t
    else:
      a[i+1] -= b[i] - a[i]
      ans += b[i]
print(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()]
sum=0
for i in range(0,len(b)):
    if(b[i]>=a[i]):
        sum=sum+a[i]
        b[i]=b[i]-a[i]
        if(b[i]>=a[i+1]):
            sum=sum+a[i+1]
            a[i+1]=0
        elif(b[i]<a[i+1]):
            sum=sum+b[i]
            a[i+1]=a[i+1]-b[i]
    elif(a[i]>b[i]):
        sum=sum+b[i]
print(sum)

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):
    m=min(a[i],b[i])
    ans+=m
    a[i]-=m
    b[i]-=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(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 b[i] >= a[i+1]:
            count += a[i+1]
            a[i+1] = 0
        else:
            count += b[i]
            a[i+1] -= b[i]

print(count)

Submission status: Accepted

Solution code:
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf


n = iinput()
a = iarr()
b = iarr()

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

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


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

ans = 0
for i in range(N):
    # i 番目の勇者が i 番目の街で倒せるモンスターの最大数
    ans += min(A[i],B[i])
    # i番目の勇者の余力 r
    r = max(0,B[i]-A[i])
    # i+1 番目の街のモンスターをついでに倒しておく
    d = min(A[i+1],r)
    ans += d
    A[i+1] -= d

print(ans)

Submission status: Accepted

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

assist = 0
kill = 0

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

if assist == 0:
    print(kill)
elif assist < a[-1]:
    print(kill + assist)
else:
    print(kill + a[-1])

Submission status: Accepted

Solution code:
'''
参考
https://note.com/tanon_cp/n/n6b51b9a57076
'''
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0
spl = 0                     # 1つ前の勇者の余力
B.append(0)                 # リストの要素数を調整

for i in range(N+1):
    ans += min(spl, A[i])   # まず1つ前の勇者の余力を討伐に充てる
    mst = max(0, A[i]-spl)  # モンスター残数

    ans += min(B[i], mst)
    spl = max(0, B[i]-mst)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10000000)
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue

# import fractions

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()
    B = il()

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

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

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

A.append(0)
A.append(0)

ai = A[0]
ai2 = A[1]

for i in range(N):

  bi = B[i]
  
  if ai <= bi:
    cnt += ai
    
    if ai2 <= (bi - ai):
      cnt += ai2
      ai = max(0, ai2-(bi - ai))
      ai2 = A[i+2]
    else:
      cnt += (bi - ai)
      ai = max(0, ai2-(bi - ai))
      ai2 = A[i+2]
      
  else:
    cnt += bi
    ai = A[i+1]
    ai2 = A[i+2]
  
print(int(cnt))

Submission status: Accepted

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

assist = 0
kill = 0

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

if assist == 0:
    print(kill)
elif assist < a[-1]:
    print(kill + assist)
else:
    print(kill + a[-1])

Submission status: Accepted

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

assist = 0
kill = 0

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

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

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):
    # i番目の街のモンスターを倒す
    ans += min(A[i], B[i])
    nb = B[i] - min(A[i], B[i])
    # i+1番目の街のモンスターを倒す
    ans += min(A[i+1], nb)
    A[i+1] -= min(A[i+1], nb)
print(ans)

Submission status: Accepted

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

ans = 0
now = a[0]
for i in range(n):
    ato_taoseru = b[i]
    if ato_taoseru <= now:
        ans += ato_taoseru
        now = a[i + 1]
    else:
        ans += now
        ato_taoseru -= now
        now = a[i + 1]
        if ato_taoseru <= now:
            ans += ato_taoseru
            now -= ato_taoseru
        else:
            ans += now
            now = 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
e = 0
for i, v in enumerate(b):
  if a[i] >= e:
      ans += e
      a[i] -= e
  else:
      ans += a[i]
      a[i] = 0
  e = 0
  if a[i] >= v:
      ans += v
  else:
      ans += a[i]
      e = v - a[i]
ans += min(a[-1], e)
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], B[i])
    nex=max(B[i]-A[i],0)
    ans+=min(nex, A[i+1])
    A[i+1]=max(A[i+1]-nex,0)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right


# https://atcoder.jp/contests/abc135/tasks/abc135_c
# 前から貪欲に倒していくのが最適
# ある勇者iについて, 街iをできる限り倒す。
# 余裕があったら街i+1を減らしておく

N = read_a_int()
A = read_ints()
B = read_ints()

ans = 0
for i in range(N):
    atk = B[i]
    if atk < A[i]:  # 倒しきれないとき
        ans += atk
        A[i] -= atk
    else:  # 倒し切れるとき
        ans += A[i]
        atk -= A[i]
        A[i] = 0
        ans += min(atk, A[i + 1])
        A[i + 1] = max(A[i + 1] - atk, 0)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = B*1
B.append(0)
B.append(0)

ans = 0
tamari = 0
for i in range(N+1):
  if A[i] <= tamari:
    ans += A[i]
    tamari = B[i]
  elif A[i] <= tamari + B[i]:
    ans += A[i]
    tamari = tamari+B[i]-A[i]
  else: #倒しきれない
    ans += tamari + B[i]
    tamari = 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], B[i])
    B[i] -= min(A[i], B[i])
    ans += min(A[i+1], B[i])
    A[i+1] -= min(A[i+1], B[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+=a[i]
    if a[i+1]>b[i]-a[i]:
      ans+=(b[i]-a[i])
      a[i+1]-=(b[i]-a[i])
    else:
      ans+=a[i+1]
      a[i+1]=0
  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()))
cnt = 0
for i in range(N):
    cnt += min(A[i],B[i])
    a = A[i]
    A[i] = max(a-B[i],0)
    B[i] = max(B[i]-a,0)
    cnt += min(A[i+1],B[i])
    A[i+1] = max(A[i+1]-B[i],0)
print(cnt)

Submission status: Accepted

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

    res = 0
    for i in range(n):
        tmp = min(a[i],b[i])
        res += tmp
        a[i] -= max(tmp,0)
        if b[i] > tmp:
            tmp = min(a[i+1],b[i]-tmp)
            res += tmp
            a[i+1] -= max(tmp,0)
    print(res)
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    res = 0
    for i in range(n):
        if A[i] <= B[i]:
            res += A[i]
            B[i] -= A[i]
            A[i] = 0

            if A[i + 1] <= B[i]:
                res += A[i + 1]
                B[i] -= A[i + 1]
                A[i + 1] = 0
            else:
                res += B[i]
                A[i + 1] -= B[i]
                B[i] = 0
        else:
            res += B[i]
            A[i] -= B[i]
            B[i] = 0

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

if __name__ == "__main__":
  resolve()

Submission status: Accepted

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

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

print(total_a - sum(a))

Submission status: Accepted

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

ans = 0
pre = a[0]
for ai, bi in zip(a[1:], b):
    if pre <= bi:
        ans += pre
        bi -= pre
    else:
        ans += bi
        bi = 0
    pre = ai
    if pre <= bi:
        ans += pre
        pre = 0
    else:
        ans += bi
        pre -= bi
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 idx, b in enumerate(B):
    count += min(b, A[idx])
    rest = min(A[idx + 1], b - min(b, A[idx]))
    A[idx + 1] -= rest
    count += rest
print(count)

Submission status: Accepted

Solution code:
n = int(input())
AL = list(map(int, input().split()))
BL = list(map(int, input().split()))
ans = 0
for i in range(n):
    if BL[i] <= AL[i]:
        ans += BL[i]
    elif BL[i] > AL[i] and BL[i] <= AL[i]+AL[i+1]:
        ans += BL[i]
        AL[i+1] = AL[i+1] - BL[i] + AL[i]
    else:
        ans += AL[i]+AL[i+1]
        AL[i+1] = 0
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
A = IL()
A_copy = A+[]
B = IL()

ans = 0

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

P(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
A = IL()
A_copy = A+[]
B = IL()

ans1 = 0
ans2 = 0

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

print(max(ans1,ans2))

Submission status: Accepted

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

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