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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

b += [0]

ans = 0

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

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]
    a[i]=0
    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=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
for i in range(N):
    a=min(A[i],B[i])
    B[i]-=a
    b=min(A[i+1],B[i])
    A[i+1]-=b
    ans+=a+b
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(len(B)):
  res += min(A[i], B[i])
  rem = B[i] - A[i]
  if rem > 0 and i+1 < len(A):
    res += min(A[i+1], rem)
    A[i+1] = max(0, A[i+1]-rem)

print(res)

Submission status: Accepted

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

    ind = 0
    res = 0

    for i in range(n):
        if a[i] <= b[i]:
            res += a[i]
            k = b[i]-a[i]
            res += min(k,a[i+1])

            a[i+1]=max(a[i+1]-k,0)

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.buffer.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))

n = ri()
a = rl()
b = rl()
cnt = 0
for i in range(n):
  ai = a[i]
  bi = b[i]
  if bi > ai:
    cnt += ai
    bi -= ai
    ai1 = a[i+1]
    if bi > ai1:
      cnt += ai1
      a[i+1] = 0
    else:
      a[i+1] -= bi
      cnt += bi
  else:
    cnt += bi
print(cnt)

Submission status: Accepted

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

bonus = 0
x = 0
for i in range(n+1):
    enemy = max(0, a[i] - bonus) 
    tmp = enemy - b[i]
    if tmp <= 0:
        x += a[i]
        bonus = -tmp
    else:
        x += (b[i] + bonus)
        bonus = 0

print(x)

Submission status: Accepted

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

M = sum(A)

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
 
sa = sum(a)
 
for i,b in enumerate(b):
	c = min(a[i],b)
	a[i] -= c
	a[i+1] -= min(a[i+1],b-c)
 
print(sa-sum(a))

Submission status: Accepted

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

 
sm = sum(a)
for i in range(n):
	c = min(a[i],b[i])
	a[i] -= c
	a[i+1] -= min(a[i+1],b[i]-c)
	
print(sm-sum(a))

Submission status: Accepted

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

sa = sum(a)

for i,b in enumerate(b):
	c = min(a[i],b)
	a[i] -= c
	a[i+1] -= min(a[i+1],b-c)

print(sa-sum(a))

Submission status: Accepted

Solution code:
import sys

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


def solve():

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

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

    ans = asum - sum(A)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
apple = list(map(int, input().split()))
orange = list(map(int, input().split()))
count = 0

for i in range(n):
    if orange[i] >= apple[i]:
        a = orange[i] - apple[i]
        count += apple[i]
        if apple[i+1] >= a and a != 0:
            b = apple[i + 1] - a
            apple[i+1] = b
            count += a
        elif apple[i + 1] < a and a != 0:
            count += apple[i + 1]
            apple[i + 1] = 0
        else:
            pass
    else:
        count += orange[i]
        apple[i] = 0
print(count)

Submission status: Accepted

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

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

print(all-sum(a))

Submission status: Accepted

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

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

        if Bs[i] >= As[i]:
            Bs[i] -= As[i]
            ans += As[i]
            As[i] = 0
        else:
            As[i] -= Bs[i]
            ans += Bs[i]
            Bs[i] = 0
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

after_sumA=sum(A)
after_sumB=sum(B)
#print(before_sumA-after_sumA)
print(before_sumB-after_sumB)

Submission status: Accepted

Solution code:
import math
import re
import copy
import sys

n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = 0

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

print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
A = deque(map(int, input().split()))
B = deque(map(int, input().split()))
ans = 0
tmp = A.popleft()
while A:
  b = B.popleft()
  nxt = A.popleft()
  ans += min(b, tmp + nxt)
  if b >= tmp + nxt:
    tmp = 0
  elif tmp + nxt > b >= tmp:
    tmp = tmp + nxt - b
  else:
    tmp = nxt
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from collections import Counter
import itertools
import fractions
import datetime
from decimal import Decimal
#from functools import reduce

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

# ###########################################

N=II()
A=LI()
B=LI()

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

Submission status: Accepted

Solution code:
#整数の入力
N = int(input())
#スペース区切りの整数の入力リスト
A_list = list(map(int, input().split()))
#スペース区切りの整数の入力リスト
B_list = list(map(int, input().split()))

count = 0
for i in range(N):
  # もしBよりAの方が大きい場合
  # A[i]しか倒せない
  if(A_list[i] >= B_list[i]):
    count = count + B_list[i]
    #print("1 : ",B_list[i])
    
  # A[i+1]も倒せるけど
  # A[i+1]に残る場合
  elif((B_list[i] - A_list[i]) < A_list[i+1]):
    count = count + A_list[i] + (B_list[i] - A_list[i])
    A_list[i+1] = A_list[i+1] - (B_list[i] - A_list[i])
    #print("2 : ",(B_list[i] - A_list[i])+A_list[i])
    
  # A[i+1]も全部倒せる場合
  else:
    count = count + A_list[i+1]+A_list[i]
    #print("3 : ",A_list[i+1]+A_list[i])
    A_list[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()))
total = 0
for i in range(N):
    total += min(A[i], B[i])
    B[i] -= min(A[i], B[i])
    A[i] -= min(A[i], B[i])
    total += min(A[i+1], B[i])
    A[i+1] -= min(A[i+1], B[i])

print(total)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    a=list(map(int, input().split()))
    b=list(map(int, input().split()))
    
    al = sum(a)
    for i in range(n):
        a[i] -= b[i]
        if a[i] < 0:
            a[i+1] += max(-a[i+1], a[i])
            a[i] = 0
    print(al-sum(a))
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
## C - City Savers
N = int(input()) + 1
A = list(map(int,input().split()))
B = list(map(int,input().split())) + [0]
win = 0
remain = 0
for n in range(N):
    if A[n] < remain:
        win += A[n]
        A[n] = 0
    else:
        win += remain
        A[n] -= remain

    if A[n] < B[n]:
        win += A[n]
        remain = B[n] - A[n]
    else:
        win += B[n]
        remain = 0
print(win)

Submission status: Accepted

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

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

print(s-sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
n = int(raw_input())
cities,q = collections.deque(map(int, raw_input().split(' '))), collections.deque(map(int, raw_input().split(' ')))
res = 0
while(q):
	u = q.pop()
	for _ in range(2):
		c = cities.pop()
		m = min(u, c)
		c -=m
		u -= m
		res += m
	cities.append(c)
print res

Submission status: Accepted

Solution code:
import collections
n = int(raw_input())
cities = collections.deque(map(int, raw_input().split(' ')))
q = collections.deque(map(int, raw_input().split(' ')))
res = 0
p = 0
while(q):
	w = q.pop()
	if cities[-1]:
		m = min(w, cities[-1])
		cities[-1]-=m
		w -= m
		res += m
	cities.pop()
	if cities[-1]:
		m = min(w, cities[-1])
		cities[-1]-=m
		w -= m
		res += m
print res

Submission status: Accepted

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

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

Submission status: Accepted

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

monstar = sum(A)
for i in range(n):
    A[i] -= B[i]
    A[i + 1] -= (-A[i]) if A[i] < 0 else 0
    A[i] = 0 if A[i] < 0 else A[i]
    A[i + 1] = 0 if A[i + 1] < 0 else A[i + 1]

print(monstar - sum(A))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

Submission status: Accepted

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

ans = 0

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


print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


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

    ans = 0
    tmp = 0

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

    if tmp >= a[-1]:
        ans += a[-1]
    else:
        ans += tmp

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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

ans = 0

for i in range(n):
    # 勇者が倒したあとに街に残っているモンスター
    tmp = a[i] - b[i]
    
    # n+1番目のモンスターにまで到達
    if tmp < 0 and -tmp <= a[i+1]:
        ans += b[i]
        a[i+1] += tmp
    # n+1番目のモンスターも駆逐
    elif tmp < 0 and -tmp > a[i+1]:
        ans += a[i]+a[i+1]
        a[i+1] = 0
    # n番目のモンスターで手一杯
    else:
        ans += b[i]

print(ans)

Submission status: Accepted

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

ans = 0
for i in range(N):
    # 勇者に余力がある場合
    if B[i] >= A[i]:
        ans += A[i]
        B[i] -= A[i]
        if B[i] >= A[i+1]:
            ans += A[i+1]
            A[i+1] = 0
        else:
            ans += B[i]
            A[i+1] -= B[i]
    # 勇者に余力がない場合
    else:
        ans += B[i]
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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())
monster = list(input().split())
hero = list(input().split())
monster = [int(monster[i]) for i in range(N+1)]
hero = [int(hero[i]) for i in range(N)]

count = 0
for i in range(N):
    count += min(monster[i+1] + monster[i], hero[i])
    monster[i+1] = min(monster[i+1], max(monster[i+1] - (hero[i] - monster[i]), 0))

print(count)

Submission status: Accepted

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

    for i in range(len(A)-1):
        b = B[i]
        b = max(0, b - A[i])
        A[i] = max(0, A[i] - B[i])
        A[i+1] = max(0, A[i+1] - b)

    print(sum_prev - sum(A))


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

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:
# input
n = int(input())
enemy = [int(i) for i in input().split()]
heros = [int(i) for i in input().split()]
defeat = 0

# main
for i in range(n):
  if enemy[i] > heros[i]:
    defeat += heros[i]
  else:
    defeat += enemy[i]
    extra = heros[i] - enemy[i]
    if extra >= enemy[i+1]:
      defeat += enemy[i+1]
      enemy[i+1] = 0
    else:
      defeat += extra
      enemy[i+1] -= extra

# output
print(defeat)

Submission status: Accepted

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

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

print(left)

Submission status: Accepted

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

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

print(kill)

Submission status: Accepted

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

sumA = sum(A)
for i in range(n-1,-1,-1):
    y = B[i]
    if(B[i] <= A[i+1]):
        A[i+1] -= B[i]
        B[i] = 0
    else:
        B[i] -= A[i+1]
        A[i+1] = 0
    
    if(B[i] == 0):
        continue
    if(B[i] <= A[i]):
        A[i] -= B[i]
        B[i] = 0
    else:
        B[i] -= A[i]
        A[i] = 0
subSumA = sum(A)

print(sumA - subSumA)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc135/tasks/abc135_c

N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

count_defeat = 0

for i, b in enumerate(B_list):
    if A_list[i] >= b:
        count_defeat += b

    else:
        count_defeat += A_list[i]
        remain_b = (b - A_list[i])

        if remain_b > A_list[i + 1]:
            count_defeat += A_list[i + 1]
            A_list[i + 1] = 0

        else:
            count_defeat += remain_b
            A_list[i + 1] -= remain_b

print(count_defeat)

Submission status: Accepted

Solution code:
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    num = int(input())        
    a_list = list(map(int, input().split()))
    a = sum(a_list)
    b_list = list(map(int, input().split()))

    for i in range(num):
        if a_list[i] >= b_list[i]:
            a_list[i] -= b_list[i]
            b_list[i] = 0
        else:
            b_list[i] -= a_list[i]
            a_list[i] = 0
            if a_list[i+1] >= b_list[i]:
                a_list[i+1] -= b_list[i]
            else:
                a_list[i+1] = 0
        

    print(a - sum(a_list))


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

Submission status: Accepted

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

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

total = 0

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

Submission status: Accepted

Solution code:

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
#C = [0] * len(A)
        
count = 0
#next = 0
for i in range(N):
    if B[i] <= A[i]:
        count += B[i]
    else:
        count += A[i]
        count += min(B[i]-A[i], A[i+1])
        A[i+1] = max(A[i+1]-(B[i]-A[i]), 0)
        #C[i+1] = B[i]-A[i]
        
    #print(count)
    #print(A)
    #print(B)
    #print(C)

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    num_monster = 0
    res = 0  # 余力
    for i in range(N):
        a = A[i] - res  # 前の人が倒しといてくれた分を引く
        b = B[i]
        if b <= a:  # 目の前の敵を倒し切れるかあやしい
            num_monster += b
            res = 0
        elif b <= a+A[i+1]:  # 次の敵の一部まで倒せる
            num_monster += b
            res = b - a
        else:  # 次の敵まで全部倒せる
            num_monster += a+A[i+1]
            res = A[i+1]
    print(num_monster)
main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0]*n
for i in range(n):
    if a[i] + a[i+1] >= b[i]:
        c[i] = b[i]
        if a[i] < b[i]:
            a[i+1] = a[i+1] + a[i] - b[i]
    else:
        c[i] = a[i] + a[i+1]
        a[i+1] = 0
print(sum(c))

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

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

  if i != N:
    if B[i] > A[i]:
      t = A[i]
      A[i] = 0
      B[i] -= t
    else:
      t = B[i]
      B[i] = 0
      A[i] -= t
print(sumA - sum(A))

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

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

Submission status: Accepted

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

    ans = 0
    for i, can_beat in enumerate(B):
        beat = min(A[i], can_beat)
        A[i] -= beat
        can_beat -= beat
        ans += beat

        beat = min(A[i + 1], can_beat)
        A[i + 1] -= beat
        ans += beat

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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