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

print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N = i_i()
A = i_l()
B = i_l()

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

    if A[i] < B[i]:
        if A[i+1] >= B[i] - A[i]:
            ans += B[i] - A[i]
            A[i+1] = A[i+1] - (B[i] -A[i])
        else:
            ans += A[i+1]
            A[i+1] = 0

print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N = i_i()
A = i_l()
B = i_l()

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

cnt = 0
res = 0 #余力
for i in range(n):

    ch = min(a[i],res)
    cnt += ch
    a[i] -= ch
    if b[i] > a[i]:
        cnt += a[i]
        res = b[i] - a[i]
    else:
        cnt += b[i]
        res = 0

cnt += min(a[n],res)

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

Submission status: Accepted

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

Submission status: Accepted

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

kill = 0
for x in range(len(B)):
    next_town_kill = 0
    same_town_kill = min(B[x], A[x])
    kill += same_town_kill

    remain = B[x] - A[x]
    if remain > 0:
        next_town_kill = min(remain, A[x + 1])
        kill += next_town_kill
        A[x + 1] = max(A[x + 1] - remain, 0)
print(kill)

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

print(suma - 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]
    else: #i + 1番目の街のモンスターを倒せる場合
        ans += a[i] #i番目のモンスター数を足す。
        if a[i + 1] < b[i] - a[i]: #i + 1番目の街のモンスターも倒しきれる場合
            ans += a[i + 1]
            a[i + 1] = 0
        else: #i + 1番目の街のモンスターを倒しきれない場合
            ans += b[i] - a[i]
            a[i + 1] -= b[i] - a[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

BeforeA = sum(A)

for i in range(N):
    # まずは同じ番号の町のモンスター倒す
    if B[i] - A[i] > 0:
        # 余力があるなら次の町のモンスター倒す
        B[i] -= A[i]
        A[i] = 0
        if A[i+1] - B[i] >0:
            A[i+1] -= B[i]
        else:
            A[i+1] = 0
    else:
        A[i] -= B[i]

AfterA = sum(A)

print(BeforeA - AfterA)

Submission status: Accepted

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

k=0

for i in range(n):

  if a[i]<b[i]:
    k+=a[i]
    b[i]=b[i]-a[i]
    a[i]=0

    if a[i+1]<b[i]:
      k+=a[i+1]
      b[i]=b[i]-a[i+1]
      a[i+1]=0
    else:
      k+=b[i]
      a[i+1]=a[i+1]-b[i]
      b[i]=0

  else:
    k+=b[i]
    a[i]=a[i]-b[i]
    b[i]=0

print(k)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in 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]:
        a[i + 1] -= b[i] - a[i]
        c += b[i]
    else:
        c += a[i] + a[i + 1]
        a[i + 1] = 0
print(c)

Submission status: Accepted

Solution code:
import numpy as np
import sys

n=int(sys.stdin.readline())
a=list(map(int,sys.stdin.readline().split()))
b=list(map(int,sys.stdin.readline().split()))
pre=0
cnt=0
for i in range(n):
    if a[i] <= pre:
        cnt+=a[i]
        pre=b[i]
    else:
        a1=a[i]-pre
        cnt+=pre
        if a1<=b[i]:
            cnt+=a1
            pre=b[i]-a1
        else:
            cnt+=b[i]
            pre=0
cnt+=min(pre,a[n])
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N = INT()
A = LIST()
B = LIST()
A_before = sum(A)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def myAnswer(N:int,A:list,B:list) -> int:
   total = 0
   for i in range(N):
      if(A[i] < B [i]):
         total += A[i]
         if(A[i + 1] > B[i] - A[i]):
            A[i+1] -= B[i] - A[i]
            total += B[i] - A[i]
         else:
            total += A[i + 1]
            A[i + 1] = 0
         A[i] = 0
      else:
         A[i] -= B[i]
         total += B[i]
   return total

def modelAnswer():
   tmp=1
def main():
   N = int(input())
   A = list(map(int,input().split()))
   B = list(map(int,input().split()))
   print(myAnswer(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()))
SumBefore = sum(A)
for i in range(N):
    A[i+1] = max(A[i+1] - max(B[i]-A[i],0) ,0)
    A[i] = max(A[i]-B[i],0)
SumAfter = sum(A)
print(SumBefore-SumAfter)

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    n = II()
    A = LI()
    B = LI()

    cnt = 0
    for i in range(n):
        left = min(A[i], B[i])
        B[i] -= left
        cnt += left

        # if B[i] > 0:
        right = min(A[i+1], B[i])
        A[i+1] -= right
        cnt += right
    print(cnt)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    n = II()
    A = LI()
    B = LI()

    cnt = 0
    pre = A[-1]
    for i in range(n-1, -1, -1):
        ai = pre
        aj = A[i]
        b = B[i]
        # print(ai, aj, b)
        if ai >= b:
            cnt += b
            pre = aj
        else:   # ai < b
            cnt += ai
            b = b - ai
            if aj >= b:
                cnt += b
                pre = aj - b
            else:
                cnt = cnt + aj
                pre = 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()))
total = 0
for i in range(n):
    if a[i] > b[i]:
        total += b[i]
    else:
        total += a[i]
        b[i] -= a[i]
        if a[i+1] > b[i]:
            a[i+1] -= b[i]
            total += b[i]
        else:
            total += a[i+1]
            a[i+1] = 0

print(total)

Submission status: Accepted

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

kill=0
a1=a[0]
for i in range(N):
    if a1 >= b[i]:
        kill += b[i]
        a1 = a[i+1]
    elif a1+a[i+1] >= b[i]:
        kill += b[i]
        a1 = a[i+1]+a1-b[i]
    else:
        kill += a1 +a[i+1]
        a1 = 0
print(kill)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1

""" Input
S = input().rstrip()
N, A, B = map(int, input().split())
A = np.array(input().split(), dtype=np.float64)
D = [int(input()) for _ in range(N)]
AB = [[int(x) for x in input().split()] for _ in range(N)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]
"""

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

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


print(total - sum(A))


# def main():

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

Submission status: Accepted

Solution code:
# solution
import io
import math

data=int(input())

nim=list(map(int,input().split()))

mike=list(map(int,input().split()))

counter=0

for i in range(data):
	counter+=min(nim[i],mike[i])
	mike[i]=max(mike[i]-nim[i],0)
	counter+=min(nim[i+1],mike[i])
	nim[i+1]=max(0,nim[i+1]-mike[i])

print(counter)

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

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline().rstrip())
*a, = map(int, sys.stdin.readline().split())
*b, = map(int, sys.stdin.readline().split())

def main():
    tot = 0
    for i in range(n):
        tot += min(b[i], a[i])
        b[i] = max(0, b[i] - a[i])
        tot += min(b[i], a[i+1])
        a[i+1] = max(0, a[i+1] - b[i])
    print(tot)
        
if __name__ ==  '__main__':
    main()

Submission status: Accepted

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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

import sys
# import math
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product                # product(iter, repeat=n)
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4


def main():
    mod = 1000000007                  # 10^9+7
    inf = float('inf')
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    n = ii()
    a = lmi()
    b = lmi()
    ans = 0
    for i in range(n):
        if a[i] >= b[i]:
            ans += b[i]
        else:
            ans += a[i]
            rest_power = b[i] - a[i]
            ans += min(a[i+1], rest_power)
            a[i+1] = max(0, a[i+1] - rest_power)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list
#from itertools import combinations # (string,3) 3回
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    A = readInts()
    B = readInts()
    ans = 0
    for i in range(n):
        prv = A[i] + A[i+1] # 3 + 5 = 8
        if prv - B[i] >= 0:
            if A[i] - B[i] >= 0:
                ans += B[i]
            else:
                ans += B[i]
                A[i+1] = prv - B[i]
        else:
            ans += prv
            A[i+1] = 0
    print(ans)
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
N = int(input())
monsters = list(map(int, input().split()))
heroes = list(map(int, input().split()))

damage = 0

for i in range(N):
    if monsters[i] + monsters[i + 1] <= heroes[i]:
        damage += monsters[i] + monsters[i + 1]
        monsters[i + 1] = 0
    elif monsters[i] < heroes[i]:
        damage += heroes[i]
        monsters[i + 1] -= heroes[i] - monsters[i]
    else:
        damage += heroes[i]

print(damage)

Submission status: Accepted

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

defeat = lambda Ai, Bi: min(Ai, Bi) 

total = 0
for i in range(len(B)):
    num_of_defeated = defeat(A[i], B[i])
    A[i] -= num_of_defeated
    total += num_of_defeated

    remainder = B[i] - num_of_defeated
    num_of_defeated = defeat(A[i + 1], remainder)
    A[i + 1] -= num_of_defeated
    total += num_of_defeated

print(total)

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

    remainder = B[i] - num_of_defeated
    num_of_defeated = min(A[i + 1], remainder)
    A[i + 1] -= num_of_defeated
    total += num_of_defeated

print(total)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

count += min(A[N], ad)     

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

Submission status: Accepted

Solution code:
n=int(input())
ene = list(map(int,input().split()))
power = list(map(int,input().split()))
ans=0
for i in range(n-1):
  ans+=min(power[i],ene[i])
  if ene[i]<power[i]:
    ans+=min((power[i]-ene[i]),ene[i+1])
    ene[i+1]=max(ene[i+1]-(power[i]-ene[i]),0)
ans+=min(power[-1],ene[-2])
if power[-1]>ene[-2]:
  ans+=min(ene[-1],(power[-1]-ene[-2]))
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()))
kill,surplus = 0,0

for i in range(n):
    tmp = min(a[i],surplus)
    kill += tmp
    a[i] -= tmp
    if b[i] > a[i]:
        kill += a[i]
        surplus = b[i]-a[i]
    else:
        kill += b[i]
        surplus = 0
tmp = min(a[n],surplus)
kill += tmp
print(kill)

Submission status: Accepted

Solution code:
#C
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b.append(0)
m = [0 for _ in range(n+1)]
tmp = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

monster_sum = sum(A)

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

print(monster_sum - rest_sum)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(x) for x in input().rstrip().split()]
B=[int(x) for x in input().rstrip().split()]
ans=0
now=A[0]
for i in range(N):
  if B[i]<=A[i]:
    ans+=B[i]
  else:
    if A[i]+A[i+1]<=B[i]:
      ans+=A[i]+A[i+1]
      A[i+1]=0
    else:
      ans+=B[i]
      A[i+1]=A[i+1]-(B[i]-A[i])
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np


n = int(input())
a = [i for i in list(map(int, input().split()))]
b = [i for i in list(map(int, input().split()))]

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

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, bb in enumerate(b):
        if bb >= a[i]:
            bb -= a[i]
            ans += a[i]
            if bb >= a[i+1]:
                ans += a[i+1]
                a[i+1] = 0
            else:
                ans += bb
                a[i+1] -= bb
        else:
            ans += bb
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
b = inpl()
res = 0
for i in range(n):
    if b[i] < a[i]:
        res += b[i]
    elif b[i] == a[i]:
        res += a[i]
    else:
        res += a[i]
        b[i] -= a[i]
        if b[i] < a[i+1]:
            res += b[i]
            a[i+1] -= b[i]
        else:
            res += a[i+1]
            a[i+1] = 0
print(res)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
res = 0
for i in range((n + 1)//2):
  if B[i] > A[i]:
    res += min(A[i] + A[i+1], B[i])
    A[i+1] = max(0, A[i] + A[i+1] - B[i])
    A[i] = 0
    B[i] = 0
  else:
    res += B[i]
    A[i] -= B[i]
    B[i] = 0
  if B[n-i-1] > A[n-i]:
    res += min(A[n-i] + A[n-i-1], B[n-i-1])
    A[n-i-1] = max(0, A[n-i-1] + A[n-i] - B[n-i-1])
    A[n-i] = 0
    B[n-i-1] = 0
  else:
    res += B[n-i-1]
    A[n-i] -= B[n-i-1]
    B[n-i-1] = 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):
    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
mod = 0
for i in range(N):
    tmp = A[i]
    if mod >= tmp:
        ans += tmp
        mod = B[i]
    else:
        ans += mod
        tmp = tmp - mod
        if B[i] >= tmp:
            ans += tmp
            mod = B[i]-tmp
        else:
            ans += B[i]
            mod = 0
if mod > 0:
    ans += min(mod, A[N])
print(ans)

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
A = iil()
B = iil()
rep = 0
ans = 0
for i,item in enumerate(B):
    ans += min(A[i],item+rep)
    rep = max(0, item - max(0, A[i] - rep))
print(ans+min(A[-1],rep))

Submission status: Accepted

Solution code:
import sys
N=input()
A=map(int, sys.stdin.readline().split())
B=map(int, sys.stdin.readline().split())
before=sum(B)

for i in range(N):
    if B[i]!=0:
        k=min(A[i],B[i])
        A[i]-=k
        B[i]-=k
    if B[i]!=0:
        k=min(A[i+1],B[i])
        A[i+1]-=k
        B[i]-=k
after=sum(B)
print before-after

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