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:
# abc135_c.py
# https://atcoder.jp/contests/abc135/tasks/abc135_c

# C - City Savers /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# N+1個の街があり、i 番目の街は Ai体のモンスターに襲われています。
# N人の勇者が居て、i 番目の勇者は i 番目または i+1 番目の街を襲っているモンスターを合計で Bi体まで倒すことができます。
# N人の勇者がうまく協力することで、合計して最大で何体のモンスターを倒せるでしょうか。

# 制約
#     入力は全て整数である。
#     1≤N≤105
#     1≤Ai≤109
#     1≤Bi≤109

# 入力
# 入力は以下の形式で標準入力から与えられる。
# N
# A1 A2 ... AN+1
# B1 B2 ... BN

# 出力
# 合計して倒せるモンスターの数の最大値を出力せよ。

# 入力例 1
# 2
# 3 5 2
# 4 5

# 出力例 1
# 9以下のようにモンスターを倒すと、合計 9体のモンスターを倒すことができ、このときが最大です。

#     1番目の勇者が 1 番目の街を襲っているモンスターを 2 体、2 番目の街を襲っているモンスターを 2体倒します。
#     2番目の勇者が 2 番目の街を襲っているモンスターを 3 体、3 番目の街を襲っているモンスターを 2体倒します。

# 入力例 2
# 3
# 5 6 3 8
# 5 100 8

# 出力例 2
# 22

# 入力例 3
# 2
# 100 1 1
# 1 100

# 出力例 3
# 3


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    # FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    N = int(lines[0])
    # X, A = list(map(int, lines[0].split()))
    monsters = list(map(int, lines[1].split()))
    braves = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(6):
    #     values.append(int(lines[i]))
    # valueses = list()
    # for i in range(M):
    #     valueses.append(list(map(int, lines[i+1].split())))

    cnt = 0

    for n in range(N):
        if monsters[n] >= braves[n]:
            cnt += braves[n]
            monsters[n] -= braves[n]
            log(f'cnt=[{cnt}]')
        else:
            # 今の町で倒した数
            cnt += monsters[n]
            log(f'cnt=[{cnt}]')
            # 次の町で倒す数
            monster = min(braves[n]-monsters[n], monsters[n+1])
            cnt += monster
            log(f'cnt=[{cnt}]')
            monsters[n+1] -= monster

    return [cnt]


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['2', '3 5 2', '4 5']
        lines_export = [9]
    if pattern == 2:
        lines_input = ['3', '5 6 3 8', '5 100 8']
        lines_export = [22]
    if pattern == 3:
        lines_input = ['2', '100 1 1', '1 100']
        lines_export = [3]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(3)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
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

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

print(cnt)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

print(ans)

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

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

print(ans)

Submission status: Accepted

Solution code:
N,A,B=open(0).read().rstrip().split('\n')
N=int(N)
A=list(map(int,A.split()))
B=list(map(int,B.split()))
hunt=0
for i in range(N):
    if A[i]>=B[i]:
        hunt+=B[i]
    elif A[i]+A[i+1]>=B[i]:
        hunt+=B[i]
        suplus=B[i]-A[i]
        A[i+1]=A[i+1]-suplus
    else:
        hunt+=A[i]+A[i+1]
        A[i+1]=0
print(hunt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
x = sum(a)
for i in range(len(b)):
  if a[i]+a[i+1]<b[i]:
    a[i] = 0
    a[i+1] = 0
  elif a[i]<b[i]:
    a[i+1] = a[i]+a[i+1]-b[i]
    a[i] = 0
  else:
    a[i]-=b[i]
    
print(x-sum(a))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
cnt = 0
def dm(i, j):
    d = min(a[i], b[j])
    a[i] -= d; b[j] -= d
    return d

for i in range(n):
    cnt += dm(i, i)
    cnt += dm(i + 1, i)
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
cnt = 0
for i in range(n):
    dm = min(a[i], b[i])
    b[i] -= dm; cnt += dm
    dmn = min(a[i + 1], b[i])
    a[i + 1] -= dmn; cnt += dmn
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
cnt = 0
for i in range(n):
    dm = min(a[i], b[i])
    a[i] -= dm; b[i] -= dm; cnt += dm
    dmn = min(a[i + 1], b[i])
    a[i + 1] -= dmn; b[i] -= dmn; cnt += dmn
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
total = sum(aaa)
for i in range(N - 1, -1, -1):
    if aaa[i + 1] > bbb[i]:
        aaa[i + 1] -= bbb[i]
    else:
        bbb[i] -= aaa[i + 1]
        aaa[i + 1] = 0
        aaa[i] = max(0, aaa[i] - bbb[i])
print(total - sum(aaa))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    *A, = map(int, input().split())
    *B, = map(int, input().split())

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

        if B[i]:
            beaten = min(A[i + 1], B[i])
            A[i + 1] -= beaten
            B[i] -= beaten
            ans += beaten

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())
Bs = list(mapint())

rest = 0
ans = 0
for i in range(N):
    a = As[i]
    b = Bs[i]
    if a>=rest:
        a -= rest
        ans += rest
    else:
        ans += a
        a = 0
    if b>=a:
        rest = b-a
        ans += a
    else:
        rest = 0
        ans += b

ans += min(rest, As[-1])

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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(b[i], a[i]+a[i+1])
    a[i+1] = max(0, a[i+1]-max(0, b[i]-a[i]))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

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

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

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

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

print(result)

Submission status: Accepted

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

ans = 0
rest = 0
for a, b in zip(B, A):
  b_ = b - rest
  if b_ > a:
    ans += rest + a
    rest = 0
  elif b_ > 0:
    rest = a - b_
    ans += b
  elif b_ <= 0:
    rest = a
    ans += b
b = A[-1]
if rest > b:
  ans += b
else:
  ans += rest
print(ans)

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] != 0:  # 担当の街
        if B[i] > A[i]:  # 担当の街を全滅させられる
            B[i] -= A[i]
            count += A[i]
        else:  # 担当の街で精一杯
            count += B[i]
            continue
    if A[i+1] < B[i]:  # 隣の街を全滅させられる
        count += A[i+1]
        A[i+1] = 0
    else:  # 隣の街を全滅させられない
        A[i+1] -= B[i]
        count += B[i]

print(count)

Submission status: Accepted

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

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

print(c - sum(a))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 15 05:49:43 2020

@author: Dell
"""

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#<ABC.C>
#<135>
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

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

Submission status: Accepted

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

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


if __name__ == "__main__":
    main()

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Created: Jul, 13, 2020 13:52:40 by Nobody
# $Author$
# $Date$
# $URL$
__giturl__ = "$URL$"


from sys import stdin
input = stdin.readline


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

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

  print(sum_)


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

Submission status: Accepted

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

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

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

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

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

print(S)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
# import bisect
# import numpy as np
# from collections import deque
from collections import deque
# map(int, sys.stdin.read().split())
import itertools
import heapq


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


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

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

Submission status: Accepted

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

ans = 0

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

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

print(ans)

Submission status: Accepted

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


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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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]
    a[i]-=b[i]
    b[i]=0
  elif a[i]<b[i]:
    cnt+=a[i]
    b[i]-=a[i]
    a[i]=0
    temp=b[i]
    cnt+=min(b[i], a[i+1])
    b[i]=max(b[i]-a[i+1], 0)
    a[i+1]=max(a[i+1]-temp, 0)
print(cnt)

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import stdin,stdout

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

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

    cnt=0

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:

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

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    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):
    ans += min(b[i], a[i]) + min(max(b[i] - a[i],0), a[i+1])
    a[i+1] = (a[i+1] - min(max(b[i] - a[i],0), a[i+1]))

print(ans)

Submission status: Accepted

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



    
if '__main__' == __name__:
    resolve()

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-1, -1, -1):
    cnt += min(b[i], a[i]+a[i+1])
    diff = b[i] - a[i+1]
    if diff > 0:
        a[i] = max(0, a[i]-diff)

print(cnt)

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

print(ans)

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