Cod sursa(job #3158486)

Utilizator octavian202Caracioni Octavian Luca octavian202 Data 18 octombrie 2023 20:16:52
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

ifstream fin("deque.in");
ofstream fout("deque.out");

deque<pair<int, int> > q; // val, ind
long long n, k, res = 0;

int main() {

    fin >> n >> k;
    for (int i = 1; i <= n; i++) {
        long long x;
        fin >> x;
        while (!q.empty() && q.back().first > x) {
            q.pop_back();
        }

        q.push_back(make_pair(x, i));
        while (q.front().second < i - k + 1)
            q.pop_front();

        if (i >= k) {
            res += q.front().first;
        }
    }

    fout << res;

    return 0;
}