Cod sursa(job #2668352)

Utilizator H7GhosTsdfgsd asdf H7GhosT Data 4 noiembrie 2020 20:01:45
Problema Deque Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>
using namespace std;

int main() {
    freopen("deque.in", "r", stdin);
    freopen("deque.out", "w", stdout);
    
    int n, k;
    scanf("%d%d", &n, &k);

    int ql = 0;
    int st = 0;
    const int mxN = 5e6 + 1;
    int* a = new int[mxN];
    int* q = new int[mxN];

    for (int i = 0; i < n; i++) {
        scanf("%d", a + i);
    }

    auto push = [&] (int el) {
        while (ql > st && el <= q[ql-1]) {
            ql--;
        }
        q[ql++] = el;
    };

    for (int i = 0; i < k; i++) {
        push(a[i]);
    }
    long long res = q[st];
    for (int i = k; i < n; i++) {
        if (q[st] == a[i-k]) st++;
        push(a[i]);
        res += q[st];
    }
    printf("%lld", res);
}