Cod sursa(job #2730567)

Utilizator alina225Avram Miruna alina225 Data 26 martie 2021 15:56:55
Problema Deque Scor 25
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <bits/stdc++.h>

using namespace std;

#define NMAX 5000001
ifstream f("deque.in");
ofstream o("deque.out");

int v[NMAX], deq[NMAX];

int main() {
    int n, k, start = 0, end = -1, i, suma = 0;
    f >> n >> k;

    for (i = 0; i < n; i++)
        f >> v[i];

    for (i = 0; i < n; i++) {
        while (start <= end && v[i] <= v[deq[end]])
            end--;

        deq[++end] = i;

        if (deq[start] == i - k)
            start++;
        if (i >= k - 1)
            suma += v[deq[start]];
    }

    o << suma;
    f.close();
    o.close();
    return 0;
}