Cod sursa(job #2222543)

Utilizator stefan.botezStefan Botez stefan.botez Data 17 iulie 2018 11:34:54
Problema Deque Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.49 kb
#include <iostream>
#include <deque>

using namespace std;
int n, k, x, sum, v[5000005];
deque<int> q;
int main()
{
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &v[i]);

        while(!q.empty() && v[i] <= v[q.back()])
            q.pop_back();

        q.push_back(i);

        if(q.front() == i - k)
            q.pop_front();

        if(i >= k)
            sum += v[q.front()];
    }

    printf("%d\n", sum);

    return 0;
}