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:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
t=0
y=[0]

for i in range(n):

    if a[i]-y[i] >= b[i]:
        
        t += b[i]
        y.append(0)
    
    elif a[i]-y[i]+a[i+1] <= b[i]:
        t += a[i]-y[i]+a[i+1]
        y.append(a[i+1])
    else:
        t += b[i]
        y.append(b[i]-a[i]+y[i])

print(t)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 0
for i in range(N):
    if A[i] > B[i]:
        ans += B[i]
    else:
        ans += (A[i] + (A[i+1] - max(A[i+1] - (B[i]-A[i]), 0)))
        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()))
B.append(0)
C = [0] * (N + 2)

cnt = 0

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

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

print(total)

Submission status: Accepted

Solution code:
n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
bl.append(0)

mons = 0
b0 = 0

for a, b in zip(al, bl):
    # print('a:{}, b:{}, b0:{}'.format(a, b, b0))
    if a >= (b + b0):
        mons += (b + b0)
        b0 = 0
    else:
        mons += a

        # a <= b0 -> b0=b
        # a > b0 -> b0=b-(a-b0)

        b0 = b - max(0, a-b0)

        # b0 = max(0, b-(b0-a))
    # print('mons:{}'.format(mons))

print(mons)



#
#
#
# for a, b in zip(al, bl):
#     a = max(0, a - b0)
#     if a >= b:
#         mons += b
#     else:
#         mons += a
#         b0 = b - a
#
# print(mons)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline

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

Submission status: Accepted

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

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

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
m=sum(B)
for i in range(n):
    c=min(A[i],B[i])
    A[i]-=c
    B[i]-=c
    d=min(A[i+1],B[i])
    A[i+1]-=d
    B[i]-=d
m-=sum(B)
print(m)

Submission status: Accepted

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

monster_num=sum(A)
for i in range(N):
    #girigiri
    if A[i] >= B[i]:
        A[i]-=B[i]
        B[i]=0
    #yoyuuari
    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(monster_num-sum(A))

Submission status: Accepted

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

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

print(killed)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
monster = list(map(int, input().split()))
hero = list(map(int, input().split()))

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[0]*(n+1)
b=[0]*n
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]
    b[i]=0
    a[i]-=b[i]
  else:
    ans+=min(b[i],a[i]+a[i+1])
    a[i+1]-=b[i]-a[i]
    a[i]=0
    a[i+1]=max(a[i+1],0)
  #print(a)
  #print(b)
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):
    x=min(a[i],b[i])
    a[i]-=x
    b[i]-=x
    ans+=x
    y=min(a[i+1],b[i])
    a[i+1]-=y
    b[i]-=y#not neccesary 
    ans+=y
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]
        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]
    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):
  # i番目の街のモンスターを倒せるだけ倒す
  tmp = min(A[i], B[i])
  ans += tmp
  A[i] -= tmp
  B[i] -= tmp
  
  # i+1番目の街のモンスターを倒せるだけ倒す
  tmp2 = min(A[i+1],B[i])
  ans += tmp2
  A[i+1] -= tmp2
  B[i] -= tmp2
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 25 21:14:23 2020

@author: Kanaru Sato
"""

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

Submission status: Accepted

Solution code:
n = int(input())
alst = [int(i) for i in input().split()]
blst = [int(i) for i in input().split()] + [0]
n_monster = 0
next_capa = 0
for a, b in zip(alst, blst):
	n_monster += min(a, b + next_capa)
	next_capa = max(b - max(a - next_capa, 0), 0)
print (n_monster)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(s) for s in input().split(' ')]
B = [int(s) for s in input().split(' ')] + [0]
m = 0
pa = 0
for a, b in zip(A, B):
    m1 = 0
    if pa > 0:
        m1 = min(pa, a)
        a -= m1
    m2 = min(a, b)
    pa = b - m2
    m += m1 + m2

print(m)

Submission status: Accepted

Solution code:
import os, sys, re, math

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

total = 0
for i in range(N, 0, -1):
    for j in [0, 1]:
        if A[i - j] and B[i - 1]:
            count = min(A[i - j], B[i - 1])
            A[i - j] -= count
            B[i - 1] -= count
            total += count

print(total)

Submission status: Accepted

Solution code:
n = int(input())
monsters = list(map(int, input().split(' ')))
kills = list(map(int, input().split(' ')))
monsters_orig = sum(monsters)
 
for i in range(n):
    extra_attack = kills[i] - monsters[i]
    monsters[i] = max(0, monsters[i] - kills[i])
    if extra_attack > 0:
        monsters[i+1] = max(0, monsters[i+1] - extra_attack)
    
print(monsters_orig - sum(monsters))

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

print(sum)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
tot = sum(A)
for i in range(n):
    en = A[i] + A[i+1]
    A[i] = max(A[i] -B[i],0)
    if A[i] == 0:
        A[i+1] = max(en - B[i],0)
        #print(A[i],A[i+1])
print(tot-sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    from builtins import int, map, list, print, min
    import sys
    sys.setrecursionlimit(10 ** 6)

    input = sys.stdin.readline
    input_list = (lambda: input().rstrip().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

total = 0
for i in range(n):
    temp = min(aa[i], bb[i])
    total += temp
    aa[i] -= temp
    if aa[i] < bb[i]:
        temp_n = min(bb[i]-temp, aa[i+1])
        total += temp_n
        aa[i+1] -= temp_n
print(total)

Submission status: Accepted

Solution code:
#135 ABC
N = int(input()) # sを取得し、strに値を入れる
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sum=0


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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#以下、他の人のコードを参照
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans=0
for i in range(n):
    if b[i] > a[i]:
        b[i] -= a[i]
        ans += 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]
        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 b[i] > a[i]:
        b[i] -= a[i]
        ans += 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]
        b[i]=0
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import itertools#これは色々使える組み込み関数みたいなやつ
#import math#数学的計算はこれでいける。普通に0.5乗しても計算可能
#w=input()
from operator import itemgetter
from sys import stdin
#input = sys.stdin.readline#こっちの方が入力が早いが使える時に使っていこう
#これもdpで考えるべきだったかも
N=3
M=3
i=0
j=0
k=0
ans=0


N=int(input())
#A=int(input())
#B=int(input())
#print(N)
monster_list=list(map(int, input().split(" ")))#数字の時
beat_list=list(map(int, input().split(" ")))#数字の時
#print(monster_list)
#print(beat_list)

for i in reversed(range(N+1)):#逆回しで値を代入しているよ
 #print(i)
 #print(monster_list)
 #print(beat_list)
 if i >=1:
    if beat_list[i-1]<monster_list[i]:#倒せる数がモンスターの数より少ない時
        ans=ans+beat_list[i-1]
        beat_list[i-1]=0
        monster_list[i]=monster_list[i]-beat_list[i-1]
    else:#倒せる数がモンスターの数より多い時
        beat_list[i-1]=beat_list[i-1]-monster_list[i]
        ans=ans+monster_list[i]
        monster_list[i]=0
        if beat_list[i-1]>=monster_list[i-1]:#自分の街まで処理しきれる時
            ans=ans+monster_list[i-1]
            monster_list[i-1]=0
        else:#自分の街までは無理な時
            ans=ans+beat_list[i-1]
            monster_list[i-1]=monster_list[i-1]-beat_list[i-1]
            beat_list[i-1]=0

 # else:
 #     if beat_list[i]>=monster_list[i]:
 #         ans=ans+monster_list[i]

 #     else:
 #         ans=ans+beat_list[i]
print(ans)

"1行1つの整数を入力を取得し、整数と取得する"


#number_list=list(map(int, input().split(" ")))#数字の時
#print(number_list)
"12 21 332 とか入力する時に使う"
"1行に複数の整数の入力を取得し、整数として扱う"

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#メモ
for i in number_list:#こっちの方がrage使うより早いらしい
    print(number_list[i-1])

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''
x=[]
y=[]
for i in range(N):
    x1, y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
print(x)
print(y)
"複数行に2数値を入力する形式 x座標とy座標を入力するイメージ"
'''

'''
mixlist=[]
for i in range(N):
    a,b=input().split()
    mixlist.append((int(a),b))
print(mixlist)
"複数行にintとstrを複合して入力するやつ，今回はリスト一つで処理している"
#1 a
#2 b
とかに使う
'''

'''
#array=[input().split()for i in range(N)]
#print(type(array[0][0]))
#print(array)
"正方行列にstr型の値を入力"
'''

#brray=[list(map(int, input().split(" ")))for i in range(N)]
#print(type(brray[0][0]))
#print(brray)
'''
入力
1 2
4 5
7 8
出力結果
[[1, 2], [4, 5], [7, 8]]
'''
"列数に関して自由度の高いint型の値を入力するタイプの行列"
#以下に別解を記載
#N, M = [int(i) for i in input().split()]
'''
table = [[int(i) for i in input().split()] for m in range(m)]

print(type(N))
print(N)
print(type(M))
print(M)

print(type(table))
print(table)
'''




#s=input()
#a=[int(i) for i in s]
#print(a[0])
#print([a])
#単数値．桁ごとに分割したい．入力と出力は以下の通り
#イメージとして1桁ごとにリストに値を入れているかんじ

'''
入力
1234
出力
1
[[1, 2, 3, 4]]
'''




'''
word_list= input().split(" ")
print(word_list[0])
"連続文字列の入力"
"qw er ty とかの入力に使う"
"入力すると空白で区切ったところでlistの番号が与えられる"
'''



'''
A, B, C=stdin.readline().rstrip().split()#何個でもいけることが多い
print(A)
"リストではなく独立したstr型を入れるなら以下のやり方でOK"
'''


#a= stdin.readline().rstrip()
#print(a.upper())
"aという変数に入っているものを大文字にして出力"




#なんかうまく入力されるけど
#a=[[int(i) for i in 1.strip()]for 1 in sys.stdin]
#a = [[int(c) for c in l.strip()] for l in sys.stdin]]
#print(a)
#複数行の数値を入力して正方行列を作成

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
B = lr()
answer = 0
for i in range(N):
    a, b = A[i], B[i]
    answer += min(a, b)
    if a < b:
        remain = b - a
        add = min(A[i+1], remain)
        answer += add
        A[i+1] -= add

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a_lst = [int(elem) for elem in input().split()]
b_lst = [int(elem) for elem in input().split()]

res = 0
for i in range(n+1):
  if i == 0:
    res += min(a_lst[i], b_lst[i])
    if b_lst[i] > a_lst[i]:
      b_lst[i] -= a_lst[i]
    else:
      b_lst[i] = 0
  elif i == n:
    res += min(a_lst[i], b_lst[i-1])
    if b_lst[i-1] > a_lst[i]:
      b_lst[i-1] -= a_lst[i]
    else:
      b_lst[i-1] = 0    
  else:
    res += min((b_lst[i-1] + b_lst[i]), a_lst[i])
    if b_lst[i-1] > a_lst[i]:
      b_lst[i-1] -= a_lst[i]
    elif b_lst[i] > (a_lst[i] - b_lst[i-1]):
      b_lst[i] -= (a_lst[i] - b_lst[i-1])
      b_lst[i-1] = 0
    else:
      b_lst[i] = 0
print(res)

Submission status: Accepted

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

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

sum_A = sum(A)

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

ans = sum_A - sum(A)
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=a[0]
for i in range(n):
  y=min(y,a[i])
  y+=a[i+1]
  x+=min(y,b[i])
  y=max(y-b[i],0)
print(x)

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 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]
    continue
  else:
    c += a[i]+a[i+1]
    a[i+1]=0
print(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):
    l =min(A[i],B[i])
    #A[i] -= l                       #残存モンスター数
    B[i] -= l                       #さらに勇者が倒せるモンスターの数
    cnt += l
    if B[i] > 0:                    #倒す余裕がある場合
        r = min(A[i+1],B[i])
        A[i+1] -= r                 #本来のA[i+1]から勇者が倒したモンスターrを引く
        #B[i] -= r
        cnt += r
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):
    l =min(A[i],B[i])
    #A[i] -= l                       #残存モンスター数
    B[i] -= l                       #さらに勇者が倒せるモンスターの数
    cnt += l
    if B[i] > 0:                    #倒す余裕がある場合
        r = min(A[i+1],B[i])
        A[i+1] -= r                 #本来のA[i+1]から勇者が倒したモンスターrを引く
        B[i] -= r
        cnt += r
print(cnt)

Submission status: Accepted

Solution code:
import sys
from heapq import heappush, heappop
#input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()] + [0]
ans = 0
j = 0
for i in range(n+1):
    ans += min(j + b[i], a[i])
    j = max(0, b[i] - max(0, a[i] - 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 reversed(range(n)):
    if B[i]-A[i+1] < 0:
        ans += B[i]
        A[i+1] -= B[i]
        B[i] = 0
    else:
        ans += A[i+1]
        B[i] -= A[i+1]
        A[i+1] = 0
    if B[i]-A[i] < 0:
        ans += B[i]
        A[i] -= B[i]
        B[i] = 0
    else:
        ans += A[i]
        B[i] -= A[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()))
cnt = 0
for i in reversed(range(N)):
    cnt += min(A[i+1],B[i])
    B[i] = B[i]-A[i+1]
    if(B[i] >= 0):
        cnt += min(A[i],B[i])
        A[i] = max(0,A[i]-B[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]
    else:
        ans+=a[i]
        b [i]-= a[i]
        if (a[i+1] >= b[i]):
            ans += b[i]
            a[i+1]-=b[i]
        else:
            ans += a[i + 1]
            a[i + 1] = 0
print(ans)

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

Solution code:
f=lambda:list(map(int,input().split()))
n=f()[0];l=f();c=0;t=l.pop(0)
for a,b in zip(l,f()):
  s=min(t+a,b);t=min(max(t+a-s,0),a);c+=s
print(c)

Submission status: Accepted

Solution code:
f=lambda:list(map(int,input().split()))
n=f()[0];l=f();c,t=0,l[0]
for a,b in zip(l[1:],f()):
  s=min(t+a,b);t=min(max(t+a-s,0),a);c+=s
print(c)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
for i in range(n):
    if a[i] <= b[i]:
        count += a[i]
        b[i] -= a[i]
        a[i] = 0
    else:
        count += b[i]
        a[i] -= 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 = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
ans = 0
for i in range(N):
    remain_power = B[i] - A[i]
    if remain_power <= 0:
        ans += B[i]
    else:
        ans += A[i]
        next_A = max(0, A[i+1] - remain_power)
        ans += A[i+1] - next_A
        A[i+1] = next_A
print(ans)