15678 : 연세워터파크

풀이

.

코드

#include <cstdio>
#include <algorithm>
#include <deque>
using namespace std;

typedef pair<long long, int> pli;

int n, d;
long long a, now, ans = -1e9;
deque<pli> deq;

int main() {
    scanf("%d %d", &n, &d);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a);
        while (!deq.empty() && deq.front().second + d < i) deq.pop_front();
        now = max(a, a + deq.front().first);
        ans = max(ans, now);
        while (!deq.empty() && deq.back().first <= now) deq.pop_back();
        deq.push_back(pli(now,i));
    }

    printf("%lld", ans);

    return 0;
}

아무말

백준, 백준 온라인 저지, BOJ, Baekjoon Online Judge, C, C++, 씨, 씨쁠쁠, JAVA, algorithm, 자바, 알고리즘, 자료구조, 문제, 문제 풀이, 풀이

wookje.kwon's profile image

wookje.kwon

2018-06-24 21:49

Read more posts by this author