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

a_sum = sum(a)
aa = a[::-1]
bb = b[::-1]

for i in range(n):
    aa[i + 1] = max(0, aa[i + 1] - max(0, bb[i] - aa[i]))
    aa[i] = max(0, aa[i]-bb[i])

print(a_sum-sum(aa))

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

Submission status: Accepted

Solution code:
import sys
from collections import deque, Counter, defaultdict
from fractions import gcd
input = sys.stdin.readline

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

    c1 = sum(As)

    for i in range(n):
        #モンスターを全滅させることができない場合
        if As[i]- Bs[i] > 0:
            As[i] -= Bs[i]
            Bs[i] = 0
        else:
            Bs[i] -= As[i]
            As[i] = 0

            if As[i+1] - Bs[i] > 0:
                As[i+1] -= Bs[i]
                Bs[i] = 0
            else:
                Bs[i] -= As[i]
                As[i+1] = 0
                
    c2 = sum(As)
    
    print(c1-c2)
        
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(bef-sum(A))

Submission status: Accepted

Solution code:
n = int(input())
cities = list(map(int, input().split(' ')))
braves = list(map(int, input().split(' ')))
enemy_count = sum(cities)

for i, b in enumerate(braves):
  for _ in range(2):
    b = braves[i]
    if b <= 0:
      break
    if cities[i] > 0:
      if b >= cities[i]:
        braves[i] = b - cities[i]
        cities[i] = 0
      else:
        braves[i] = 0
        cities[i] = cities[i] - b
    elif cities[i] == 0:
      if b >= cities[i+1]:
        braves[i] = b - cities[i+1]
        cities[i+1] = 0
      else:
        braves[i] = 0
        cities[i+1] = cities[i+1] - b
    else:
      raise Exception

kill_count = enemy_count - sum(cities)
print(kill_count)

Submission status: Accepted

Solution code:
N = int(input())
A = [-1] + [int(a) for a in input().split()]
B = [-1] + [int(b) for b in input().split()]

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

print(count)

Submission status: Accepted

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

ms = 0

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

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

print(ms)

Submission status: Accepted

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

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

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

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

def main():
    n = ri()
    la = rli()
    lb = rli()
    ans = 0
    for i in range(n):
        if la[i] <= lb[i]:
            ans += la[i]
            lb[i] -= la[i]
        else:
            ans += lb[i]
            lb[i] = 0
        b2 = min(lb[i], la[i+1])
        ans += b2
        la[i+1] -= b2
    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()))
pf = min(a[0], b[0])
f = pf
pf = min(a[1], b[0] - pf)
f += pf
for i in range(1, n):
    pf = min(a[i] - pf, b[i])
    f += pf
    pf = min(a[i + 1], b[i] - pf)
    f += pf
print(f)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))  # 初期のモンスターの数
b = list(map(int, input().split()))  # 各勇者が倒せるモンスターの数
num = 0
for i in range(n):
    # 倒したモンスターの数をnumに追加
    num += min(a[i], b[i])
    if b[i] - a[i] > 0:  # モンスターが残っていないとき
        # 隣町の勇者が倒せるモンスターの数か，隣町のモンスター全てか．
        num += min(b[i] - a[i], a[i + 1])
        # 隣町のモンスター数は残っているか，全滅しているか．
        a[i + 1] = max(a[i+1]-(b[i] - a[i]), 0)
print(num)

Submission status: Accepted

Solution code:
def main():
    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])
        # 勇者iがまだi+1番目のモンスターを倒せる場合
        if b[i] > a[i]:
            # 残りの倒せる体数かi+1番目のモンスター全部
            ans += min(b[i] - a[i], a[i+1])
            # 残りのモンスターの数 か 全部倒された
            a[i+1] = max(a[i+1] - (b[i] - a[i]), 0)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
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) :
    sA = A[i] + A[i+1]
    if sA >= B[i] :
        d = B[i] - A[i]
        if d >= 0:
            A[i] = 0
            A[i+1] -= d
        else :
            A[i] -= B[i]
        ans += B[i]
    else :
        A[i] = 0
        A[i+1] = 0
        ans += sA
print(ans)

Submission status: Accepted

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


def solve(N: int, A: "List[int]", B: "List[int]"):
    B.append(0)
    ans = 0
    tmp = 0

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

    print(ans)

    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):
    if (A[i] <= B[i]):
        ans += A[i]
        B[i] -= A[i]
        if (A[i + 1] > B[i]):
            A[i + 1] -= B[i]
            ans += B[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, = map(int, input().split())
*b, = map(int, input().split())

knock = 0
ex = 0
for i in range(n):
    if a[i] < b[i]:
        ex = b[i] - a[i]
        knock += a[i]
    elif a[i] >= b[i]:
        ex = 0
        knock += b[i]

    if ex != 0:
        if a[i+1] < ex:
            knock += a[i+1]
            a[i+1] = 0
        else:
            a[i+1] -= ex
            knock += ex

print(knock)

Submission status: Accepted

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

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

print(Ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
B.append(0)
sumA=sum(A)
c=min(A[0],B[0])
A[0]-=c
B[0]-=c

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

print(sumA-sum(A))

Submission status: Accepted

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

sum_v = 0

for i in range(n):
    sum_v += min(a[i], b[i])
    if b[i] - a[i] > 0:
        sum_v += min(b[i] - a[i], a[i+1])
        a[i+1] = max(a[i+1] - (b[i] - a[i]), 0)
        

#sum_v += min(b[n-1] - a[n-1], a[n])

print(sum_v)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
cnt = dif = 0
for i in range(n+1):
    cnt += min(dif,a[i])
    if i!=n:
        a[i] -= min(dif,a[i])
        cnt += min(a[i],b[i])
        dif = max(0,b[i]-a[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):
    if a[i] >= b[i]:
        ans+=b[i]
    elif a[i] + a[i+1] <= b[i]: # kill all
        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:
# import itertools
# import math

import sys
input = sys.stdin.readline


def solve(N, A, B):
    defeat_monstar = 0
    for nn in range(N):
        # 最初の街のモンスターのみ倒せる場合
        if A[nn] >= B[nn]:
            defeat_monstar += B[nn]
        else:
            B[nn] -= A[nn]
            # 次の街のモンスターも全て倒せる場合
            if A[nn + 1] <= B[nn]:
                defeat_monstar += A[nn] + A[nn + 1]
                A[nn + 1] = 0
            else:
                A[nn + 1] = A[nn + 1] - B[nn]
                defeat_monstar += A[nn] + B[nn]

    return defeat_monstar


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

# P = []
# S = []
# for i in range(N):
#     pp, ss = map(int, input().split())
#     P.append(pp)
#     S.append(ss)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from functools import reduce

def main():
    N = int(input())
    A = [int(c) for c in input().split()]
    B = [int(c) for c in input().split()]

    def save(i):
        n = min(A[i], B[i])
        m = 0
        if B[i] > A[i]:
            m = min(A[i+1], B[i] - A[i])
            A[i+1] -= m
        return n+m

    print(reduce(lambda total, i: total + save(i), range(N), 0))


main()

Submission status: Accepted

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

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

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

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
a_list.reverse()
b_list.reverse()
total = 0
res = 0

for i in range(len(a_list)):
    if i == 0:
        val_1 = a_list[0]
        val_2 = b_list[0]
        if val_1 <= val_2: # complete
            total += val_1
            res = val_2 - val_1
        else:
            total += val_2
            res = 0
            
    elif i == len(a_list) - 1:
        if res >= a_list[i]:
            total += a_list[i]
        else:
            total += res
        
    else:
        val_1 = a_list[i]
        val_2 = b_list[i] + res
        if  val_1 <= val_2: # complete
            total += val_1
            res = val_2 - val_1
            if res > b_list[i]:
                res = b_list[i]
        else:
            total += val_2
            res = 0
            
print(total)

Submission status: Accepted

Solution code:
# 初案のアルゴリズムが違っていた。難しかった。

n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
b.append(0) #ダミー
z = 0
c = b[0]
for i in range(n+1):
    x = a[i] - c
    if i < n:
        if x <= 0:
            z += a[i]
            if -x >= a[i+1]:
                c = b[i+1] + a[i+1]
            elif -x < a[i+1]:
                c = b[i+1] - x
        elif x > 0:
            z += c
            c = b[i+1]
    elif i == n:
        if x <= 0:
            z += a[i]
        else:
            z += c
print(z)

Submission status: Accepted

Solution code:
#

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

Submission status: Accepted

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

print(r - sum(a))

Submission status: Accepted

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

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

    else:
        ans+=A[i]+A[i+1]
        A[i]=0
        A[i+1]=0

print(ans)

Submission status: Accepted

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

ans = 0
p = 0

for index, i in enumerate(A):
  if index > len(B) - 1:
    if p > A[index]:
      ans += A[index]
    else:
      ans += p
      
    break

  if A[index] - (B[index] + p) < 0:
    ans += A[index]
    if p > A[index]:
      p = B[index]
    else:
      p = abs(A[index] - B[index] - p)
      
    continue

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
Monster = list(map(int, input().split()))
Brave = list(map(int, input().split()))
cnt = 0
amari = 0

for i in range(N):

    cnt += min(amari, Monster[i])
    Monster[i] = max(0, Monster[i]-amari)
    cnt += min(Monster[i], Brave[i])
    amari = max(0, Brave[i]-Monster[i])

cnt += min(amari, Monster[N])
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import _collections
import string
from functools import lru_cache

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

MOD = int(1e09) + 7

def main():
    N = cin.int()
    A = cin.mapInt()
    B = cin.mapInt()

    ans = sum(A)
    for i in range(N):
        n = min(A[i],B[i])
        A[i]-=n
        B[i]-=n
        n = min(A[i + 1],B[i])
        A[i + 1] -= n
        B[i]-=n
    ans -= sum(A)
    print(ans)
    return

if __name__ == "__main__":
    main()

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

Submission status: Accepted

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

    init_sum = sum(machi_list)

    for i in range(n):
        machi_list[i] -= yusha_list[i]
        if machi_list[i] < 0:
            machi_list[i+1] += machi_list[i]
            if machi_list[i+1] < 0:
                machi_list[i+1] = 0
            machi_list[i] = 0

    print(init_sum-sum(machi_list))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A=[0]*(n+1)
sum1=sum(a)
for i in range(n):
	A[i]=max(0,a[i]-b[i])
	A[i+1]=max(0,min(a[i+1]-b[i]+a[i],a[i+1]))
	a[i]=A[i]
	a[i+1]=A[i+1]
print(int(sum1)-int(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(a[i]>=b[i]):
        ans+=b[i]
    elif(a[i+1]<b[i]-a[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()))

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

Submission status: Accepted

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

counter = 0
for i in range(n):
  d = min(A[i], B[i])
  A[i] -= d
  B[i] -= d
  counter += d
  d = min(A[i+1], B[i])
  A[i+1] -= d
  B[i] -= d
  counter += d
print(counter)

Submission status: Accepted

Solution code:
f = lambda : list(map(int, input().split()))
n = int(input())
la = f()
lb = f()

tmp = 0
ans = 0

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

print(ans + min(la[n], tmp))

Submission status: Accepted

Solution code:
N = int(input())

monsters = [int(i) for i in input().split()]
savers = [int(i) for i in input().split()]

cnt = 0
for i in range(N):
    first_match = savers[i] - monsters[i]
    if first_match > 0:
        cnt += monsters[i]
        monsters[i] = 0
        
        second_match = first_match - monsters[i+1]
        
        if second_match >= 0:
            cnt += monsters[i+1]
            monsters[i+1] = 0
        else :
            cnt += first_match
            monsters[i+1] = abs(second_match)
    else :
        cnt += savers[i]
        monsters[i] = abs(first_match)

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
monster = list(map(int, input().split()))
savers = list(map(int, input().split()))
first = sum(monster)
rest = []
for i in reversed(range(n)):
    monster_i = monster.pop()
    save_i = savers[i]
    # 倒したモンスター
    monster_ii = max(monster_i - save_i, 0)
    # 残り倒せるかず
    save_ii = save_i - (monster_i - monster_ii)
    rest.append(monster_ii)
    monster[-1] = max(monster[-1] - save_ii, 0)
ans = first - sum(rest) - monster[0]
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(n):
    if al[i] > bl[i]:
        ans += bl[i]
    elif al[i] + al[i+1] > bl[i]:
        ans += bl[i]
        al[i+1] = al[i+1] + al[i] - bl[i]
    else:
        ans += al[i] + al[i+1]
        al[i+1] = 0
        
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ai=[]
for i in map(int,input().split()):
    ai.append(i)
bi=[]
for i in map(int,input().split()):
    bi.append(i)

asum=0
for i in range(n+1):
    asum=asum+ai[i]    

i=0
if ai[i]<=bi[i]:
    bi[i]=bi[i]-ai[i]
    ai[i]=0
else :
    ai[i]=ai[i]-bi[i]
    bi[i]=0
for i in range(1,n,1):
    if ai[i]<=bi[i-1]:
        bi[i-1]=bi[i-1]-ai[i]
        ai[i]=0
    else :
        ai[i]=ai[i]-bi[i-1]
        bi[i-1]=0
    if ai[i]<=bi[i]:
        bi[i]=bi[i]-ai[i]
        ai[i]=0
    else :
        ai[i]=ai[i]-bi[i]
        bi[i]=0
i=n
if ai[i]<=bi[i-1]:
    bi[i-1]=bi[i-1]-ai[i]
    ai[i]=0
else :
    ai[i]=ai[i]-bi[i-1]
    bi[i-1]=0

asum2=0
for i in range(n+1):
    asum2=asum2+ai[i]    

print(asum-asum2)

Submission status: Accepted

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

Sum = sum(a)

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

Submission status: Accepted

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

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


ans=t-sum(a)        
        
print(ans)

Submission status: Accepted

Solution code:
def LI():
  return [int(s) for s in input().split()]

N = int(input())

A_s = LI()
B_s = LI()

counter = 0

for i in range(N):
  if A_s[i] > B_s[i]:
    counter+=B_s[i]
  elif B_s[i] < A_s[i] + A_s[i+1]:
    counter+=B_s[i]
    A_s[i+1] = A_s[i+1] - (B_s[i]-A_s[i])
  else:
    counter+=A_s[i]+A_s[i+1]
    A_s[i+1] = 0
    
print(counter)

Submission status: Accepted

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

Submission status: Accepted

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

zan = a[0]
kill = 0
for i in range(n):
    kill_first_town = min(zan, b[i])
    if kill_first_town == zan:
        kill_second_town = min(b[i]-zan, a[i+1])
    else:
        kill_second_town = 0
    zan = a[i+1] - kill_second_town    
    kill += kill_first_town + kill_second_town
print(kill)

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):
    temp = A[i]
    A[i] = max(0,A[i]-B[i])
    B[i] = max(0,B[i]-temp)
    A[i+1] = max(0,A[i+1]-B[i])
after=sum(A)
print(before-after)

Submission status: Accepted

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

before=sum(A)
for n in range(N):
    temp = A[n]
    A[n] = max(0, A[n]-B[n])
    B[n] = max(0, B[n]-temp)
    A[n+1] = max(0, A[n+1]-B[n])
after=sum(A)
print(before-after)

Submission status: Accepted

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

cnt = 0
for i in range(len(b_nums)):
    b_num = b_nums[i]
    m_num = m_nums[i]
    cnt += min(b_num, m_num)
    if b_num > m_num:
        tmp = b_num - m_num
        if tmp > m_nums[i+1]:
            cnt += m_nums[i+1]
            m_nums[i+1] = 0
        else:
            cnt += tmp
            m_nums[i+1] -= tmp

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
Blist = list(map(int, input().split()))
amari = 0
ans = 0
for i in range(N):
    can_kill = Blist[i]
    enemy = Alist[i]
    if can_kill > enemy:
        Alist[i] = 0
        ans += enemy
        amari = Blist[i] - enemy
    else:
        Alist[i] -= can_kill
        ans += can_kill
        amari = 0
    if Alist[i + 1] > amari:
        ans += amari
        Alist[i + 1] -= amari
    else:
        ans += Alist[i + 1]
        Alist[i + 1] = 0
    # print(Alist, ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
remain=A[0]
ans1=0
for i in range(n):
    if B[i]<remain:
        ans1+=B[i]
        remain=A[i+1]
    else:
        ans1+=remain
        yoryoku=B[i]-remain
        if yoryoku>A[i+1]:
            ans1+=A[i+1]
            remain=0
        else:
            ans1+=yoryoku
            remain=A[i+1]-yoryoku
print(ans1)

Submission status: Accepted

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

    ans = 0
    for i in reversed(range(N+1)):
        if i == N:
            ans += min(B[i-1],A[i])
            B[i-1] = max(0,B[i-1]-A[i])
        elif i == 0:
            ans += min(A[i],B[i])
        else:
            ans += min(B[i],A[i])
            A[i] = max(0,A[i]-B[i])
            ans += min(B[i-1],A[i])
            B[i-1] = max(0,B[i-1]-A[i])
        ##print(*A)
        ##print(*B)


    print(ans)


if __name__=="__main__":
    main()