목차

연세워터파크

ps
링크acmicpc.net/…
출처BOJ
문제 번호15678
문제명연세워터파크
레벨플래티넘 5
분류

DP, monotone queue

시간복잡도O(n)
인풋사이즈n<=10^5
사용한 언어Python
제출기록42440KB / 176ms
최고기록176ms
해결날짜2022/07/02

풀이

코드

"""Solution code for "BOJ 15678. 연세워터파크".

- Problem link: https://www.acmicpc.net/problem/15678
- Solution link: http://www.teferi.net/ps/problems/boj/15678

Tags: [DP] [Monotone queue]
"""

import collections

INF = float('inf')


def main():
    N, D = [int(x) for x in input().split()]
    K = [int(x) for x in input().split()]

    deq = collections.deque([-INF])
    dp = [-INF] * D
    for dp_left, k_i in zip(dp, K):
        dp_cur = k_i + max(0, deq[0])
        while deq and deq[-1] < dp_cur:
            deq.pop()
        deq.append(dp_cur)
        dp.append(dp_cur)
        if deq[0] == dp_left:
            deq.popleft()

    print(max(dp))


if __name__ == '__main__':
    main()