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:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

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

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

print(sm - sum(A))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - City Savers
https://atcoder.jp/contests/abc135/tasks/abc135_c

"""
import sys


def solve(N, monsters, heros):
    carries = [0] * (N + 1)
    ans = 0
    for i in range(N+1):
        m = monsters[i]
        x = min(carries[i], m)
        ans += x
        m -= x
        h = heros[i]
        y = min(h, m)
        ans += y
        if y < h:
            carries[i+1] = h - y
    return ans


def main(args):
    N = int(input())
    monsters = [int(a) for a in input().split()]
    heros = [int(b) for b in input().split()] + [0]
    ans = solve(N, monsters, heros)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[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] + min(b[i] - a[i], a[i+1])
        a[i+1] = max(0, a[i+1] - (b[i] - a[i]))

print(ans)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    c = sum(A)
    for i in range(N):
        a = min(A[i], B[i])
        A[i] -= a
        A[i+1] -= min(A[i+1], B[i] - a)
    return c - sum(A)

print(solve())

Submission status: Accepted

Solution code:
def defeat_monster(a, b, i, j):
    defeated = min(a[i], b[j])
    a[i] -= defeated
    b[j] -= defeated
    return defeated

n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
count = 0
for i in range(n):
    count += defeat_monster(a, b, i, i)
    count += defeat_monster(a, b, i + 1, i)
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
ans = 0
for i in range(N):
    ans += B_list[i] if A_list[i]-B_list[i]>=0 else A_list[i]
    tmp = min(B_list[i], A_list[i])
    B_list[i]-= tmp
    A_list[i]-= tmp
    if B_list[i]>0:
        ans += B_list[i] if A_list[i+1]-B_list[i]>=0 else A_list[i+1]
        A_list[i+1]-= min(B_list[i], A_list[i+1])
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] + a[i+1]:
        count += a[i] + a[i+1]
        a[i+1] = 0
    elif b[i] > a[i] and b[i] < a[i] + a[i+1]:
        count += b[i]
        a[i+1] -= b[i] - a[i]
    elif a[i] >= 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()))
ans = 0

if A[0] > B[0]:
    ans += B[0]
    B[0] = 0
else:
    ans += A[0]
    B[0] = B[0] - A[0]

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

if A[N] > B[N - 1]:
    ans += B[N - 1]
else:
    ans += A[N]

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, atc in enumerate(b):
    if a[i] >= atc:
        a[i] = a[i] - atc
        count+=atc
    else:
        atc = atc - a[i]
        count+=a[i]
        a[i] = 0
        if a[i+1] >= atc:
            a[i+1] = a[i+1] - atc
            count+=atc
        else:
            atc = atc - a[i+1]
            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()))
e=0
for i in range(1,n+1):
  t=min(a[-i],b[-i])
  u=min(a[--~i],b[-i]-t)
  e+=t+u
  a[--~i]-=u
print(e)

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):
    if A[i] >= B[i]:
        res += B[i]
    else:
        res += A[i]
        x = min(A[i+1], B[i]-A[i])
        A[i+1] -= x
        res += x

print(res)

Submission status: Accepted

Solution code:
#abc135 c
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]>=0:
        ans+=b[i]
    else:
        ans+=a[i]
        b[i]=b[i]-a[i]
        if a[i+1]-b[i]>=0:
            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, num = 0, 0
for i in range(N):
  ans += min(B[i], A[i]+A[i+1])
  d = B[i] - A[i]
  if d > 0:
    A[i+1] -= min(d, A[i+1])
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):
    mi = a[i] - b[i]
    ans += min(b[i], a[i]+a[i+1])
    if mi < 0:
        a[i+1] += mi
        if a[i+1] < 0:
            a[i+1] = 0
print(ans)

Submission status: Accepted

Solution code:
import sys
import os


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

    N = int(sys.stdin.readline().rstrip())
    city = list(map(int, sys.stdin.readline().split()))
    brave = list(map(int, sys.stdin.readline().split()))

    init = sum(city)
    for n in range(N):
        bef = city[n]
        city[n] -= min(city[n], brave[n])
        city[n+1] -= min(city[n+1], brave[n]-min(bef, brave[n]))

    print(init-sum(city))


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):
    ans += min(B[i],A[i]+A[i+1])
    if 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 = [int(m) for m in input().split(" ")]
B = [int(b) for b in input().split(" ")]

Sum = 0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
ans = 0
for i in range(n):
    if a_list[i] + a_list[i + 1] > b_list[i]:
        ans += b_list[i]
        a_list[i + 1] = min(a_list[i + 1], a_list[i] + a_list[i + 1] - b_list[i])
    else:
        ans += a_list[i] + a_list[i + 1]
        a_list[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]:
		count += a[i]
		b[i] = b[i]-a[i]
		a[i] = 0
		count += min(b[i],a[i+1])
		a[i+1] = a[i+1] - min(b[i],a[i+1])
	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
if b[0] >= a[0]:
    count = a[0]
    b[0] -= a[0]
else:
    count = b[0]
    b[0] = 0
    
for i in range(1, n):
    if a[i]==0: continue
    if b[i-1] >= a[i]:
        count += a[i]
        continue
    if b[i] >= a[i] - b[i-1]:
        count += a[i]
        b[i] -= (a[i] - b[i-1])
        continue
    count += b[i-1] + b[i]
    b[i] = 0

count += min(a[-1], b[-1])

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 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())
# 配列入力の受け取り
arrA = list(map(int,input().split()))
arrB = list(map(int,input().split()))

mons = arrA[n]
res = 0
for i in range(n-1,-1,-1):
    mons += arrA[i]
    if mons>=arrB[i]:
        res += arrB[i]
        mons = min(mons-arrB[i],arrA[i])
    else:
        res += mons
        mons = 0

print(res)

Submission status: Accepted

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

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

print(sumMonster-sum(A))

Submission status: Accepted

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

ans = sum(A)

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

    if A[i] > 0 and i != N + 1:
        if A[i] - B[i] > 0:
            A[i] -= B[i]
            B[i] = 0
        else:
            B[i] -= A[i]
            A[i] = 0

print(ans - sum(A))

Submission status: Accepted

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


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


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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import sys

m=10**9 + 7 
sys.setrecursionlimit(1000010)
N = int(input())
A  = list(map(int,input().split()))
B  = list(map(int,input().split()))

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

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

sa = min(A[N],B[N-1])
ans = ans + sa
A[N] = A[N] - sa
B[N-1] = B[N-1] - sa
print(ans)

Submission status: Accepted

Solution code:
#135_C
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]:
        if a[i+1]>(b[i]-a[i]):
            cnt+=b[i]
            a[i+1]-=(b[i]-a[i])
        else:
            cnt+=a[i]+a[i+1]
            a[i+1]=0
    else:
        cnt+=b[i]
print(cnt)

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

print(mon)

Submission status: Accepted

Solution code:
n = int(input())
list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))

ans = 0
re = 0

for i in range(n):
    if list_A[i] - list_B[i] - re >= 0:
        ans += list_B[i] + re
        re = 0
    else:
        ans += list_A[i]
        if list_A[i]-re >= 0:
            re = re + list_B[i] - list_A[i]
        elif list_A[i]-re < 0:
            re = list_B[i]


if list_A[n]-re >= 0:
    ans += re
else:
    ans += list_A[n]

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import stdin

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

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

Submission status: Accepted

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

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

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):
  x=min(a[i],b[i])
  a[i]-=x
  b[i]-=x
  y=min(a[i+1],b[i])
  a[i+1]-=y
  b[i]-=y
  cnt+=x+y
print(cnt)

Submission status: Accepted

Solution code:

n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cnt = 0
for i in range(n):
    cnt += min(A[n - i], B[n - 1 - i])
    B[n - 1 - i] -= min(A[n - i], B[n - 1 - i])
    if(B[n - 1 - i] != 0):
        cnt += min(A[n - i - 1], B[n - 1 - i])
        A[n - i - 1] -= min(A[n - i - 1], B[n - 1 - i])

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

print(ans)

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

D = A[-2] - B[-1]    
A[-2] = D

if A[-2] < 0:
        A[-2] = 0
        A[-1] = A[-1] - (-D)
        if A[-1] < 0:
            A[-1] = 0
Y = sum(A)
print(X-Y)

Submission status: Accepted

Solution code:
# coding: utf-8

n = int(input().rstrip())

monster_list = list(map(int, input().rstrip().split(" ")))
yusya = list(map(int, input().rstrip().split(" ")))

kill_count = list(range(n))

for i in range(n):
    tmp = min(yusya[i], monster_list[i])
    if yusya[i] > tmp:
        tmp2 = min(yusya[i] - tmp, monster_list[i+1])
        monster_list[i+1] -= tmp2
        tmp += tmp2
    kill_count[i] = tmp

ans = sum(kill_count)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
b=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
f=sum(a)
for i in range(n):
    a1=a[i]
    a[i]=max(0,a[i]-b[i])
    b[i]=max(0,b[i]-a1)
    a2=a[i]
    a[i]=max(0,a[i]-b[i+1])
    b[i+1]=max(0,b[i+1]-a2)
print(f-sum(a))

Submission status: Accepted

Solution code:
n=int(input())
ans=0
a=list(map(int,input().split()))
b=list(map(int,input().split()))
for i in range(n):
    x=min(a[i],b[i])
    ans+=x
    b[i]-=x
    x=min(a[i+1],b[i])
    ans+=x
    a[i+1]-=x
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-1, -1, -1):
    # ans += max(min(A[i], B[i]), min(A[i+1], B[i]), min(A[i]+A[i+1], B[i]))
    if A[i] + A[i+1] <= B[i]: # どちらも倒しきれる場合
        ans += A[i] + A[i+1]
        A[i], A[i+1] = 0, 0
    else: # どちらかが倒しきれない場合
        ans += B[i] # 倒せるのはB[i]体のみ
        A[i] = A[i] + min(A[i+1] - B[i], 0) 
print(ans)

Submission status: Accepted

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split())) + [0]
    C = [0] * (N + 1)

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

    print(result)


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

Submission status: Accepted

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

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

   if(b[i] > a[i]):
      ans += a[i]
      left = b[i] - a[i]
   else:
      ans += b[i]
      left = 0

ans += min(a[n], left)
print(ans)

Submission status: Accepted

Solution code:
#import math
#import bisect
#import numpy as np
#import itertools
#import copy
import sys

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

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

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

    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()))
s=0

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

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

Submission status: Accepted

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


num = sum(B)

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(num - sum(B))

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]
        if b[i]-a[i] > a[i+1]:
            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 = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

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

print(cnt)

Submission status: Accepted

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

cnt = min(a[0], b[0])
b[0] -= min(a[0], b[0])
for i in range(1, n):
    if b[i-1] > 0:
        cnt += min(a[i], b[i-1])
        a[i] -= b[i-1]
    if a[i] > 0:
        cnt += min(a[i], b[i])
        b[i] -= min(a[i], b[i])
cnt += min(a[-1], b[-1])
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):
  f = min(a[i],b[i])
  ans += f
  a[i] -= f
  g = min(a[i+1],b[i]-f)
  ans += g
  a[i+1] -= g
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):
    s1=A[i]
    s2=A[i+1]
    if s1-B[i]>0:   #i街で勇者君は心置きなくぶっ倒せる
        cnt=cnt+B[i]
        s1=s1-B[i]
    else:   #s1-B[i]<=0i街で勇者君は倒したりない
        cnt=cnt+s1
        B[i]=B[i]-s1
        s1=0
        if s2-B[i]>0:   #i+1街で勇者君は心置きなくぶっ倒せる
            cnt=cnt+B[i]
            s2=s2-B[i]
            B[i]=0
        else:           #i+1でも倒したりなかった勇者君
            cnt=cnt+s2
            B[i]=B[i]-s2
            s2=0
    A[i]=s1
    A[i+1]=s2
    #print(A,B)
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):
	ans += min([A[i], B[i]])
	B[i] = max([0, B[i] - A[i]])
	ans += min([A[i+1], B[i]])
	A[i+1] = max([0, 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()))

x = 0
y = 0
for i in range(n):
  t = min(a[i], x)
  y += t
  a[i] -= t
  if b[i] > a[i]:
    y += a[i]
    x = b[i]-a[i]
  else:
    y += b[i]
    x = 0
  
t = min(a[n], x)
y += t
print(y)

Submission status: Accepted

Solution code:
def Main(N, A, B):
    ans = 0
    remain = 0
    for i in range(N):
        if A[i] >= B[i]:
            ans += B[i]
        else: # A[i] < B[i]
            ans += A[i]
            remain = B[i]-A[i]
            next_attack = A[i+1]-remain
            if next_attack > 0:
                ans += remain
                A[i+1] -= remain
            else:
                ans += A[i+1]
                A[i+1] = 0
    return ans
        

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
ans = 0
mons = 0
yusha = 0
for i in range(N):
    mons = max(A[i] - yusha, 0)
    ans += min(yusha, A[i])
    ans += min(mons, B[i])
    yusha = max(0, B[i] - mons)
ans += min(yusha, A[N])
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n = int(input())
  As = list(map(int, input().strip().split()))
  Bs = list(map(int, input().strip().split()))
  ne = sum(As)
  for i in range(n):
    if As[i] >= Bs[i]:
      As[i] -= Bs[i]
    else:
      As[i+1] = max(As[i+1] - (Bs[i] - As[i]), 0)
      As[i] = 0
  print(ne - sum(As))
    
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]:
        B[i] -= A[i]
        cnt += A[i]
        cnt += min(B[i],A[i+1])
        A[i+1] -= min(B[i],A[i+1])
    else:
        cnt += B[i]
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 = sum(B)
for i in range(N):
  if A[i] <= B[i]:
    B[i] -= A[i]
    A[i] = 0
  elif A[i] > B[i]:
    A[i] -= B[i]
    B[i] = 0
  if A[i+1] <= B[i]:
    B[i] -= A[i+1]
    A[i+1] = 0
  elif A[i+1] > B[i]:
    A[i+1] -= B[i]
    B[i] = 0
print(ans - sum(B))

Submission status: Accepted

Solution code:
#city savers
n=int(input())
ans=0
a=list(map(int,input().split()))
b=list(map(int,input().split()))
for i in range(n):
    if a[i]>=b[i]:
        ans+=b[i]
        a[i]-=b[i]
    else:
        #a[i]<b[i]の時
        ans+=a[i]
        b[i]=b[i]-a[i]
        a[i]=0
        if a[i+1]>=b[i]:
            ans+=b[i]
            a[i+1]=a[i+1]-b[i]
 
        else:
            #a[i+1]<b[i]の時
            ans+=a[i+1]
            a[i+1]=0

print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *AB = map(int, read().split())
A = AB[:N + 1]
B = AB[N + 1:]

B.append(0)
ans = 0
rest = 0
for i, j in zip(A, B):
    x = min(rest, i)
    ans += x
    i -= x
    if i < j:
        ans += i
        rest = j - i
    else:
        ans += j
        rest = 0
print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *AB = map(int, read().split())
A = AB[:N + 1]
B = AB[N + 1:]

ans = 0
cnt = 0
for i, j in zip(A, B):
    # print(f'A:{i} B:{j} cnt:{cnt}')
    ans += min(i, cnt)
    m = min(i-cnt, j)
    m=max(m,0)
    ans += m
    cnt = max(j - m, 0)
ans += min(A[-1], cnt)

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

print(c)