Cod sursa(job #3151725)

Utilizator TeddyDinutaDinuta Eduard Stefan TeddyDinuta Data 22 septembrie 2023 17:39:08
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("deque.in");
ofstream out("deque.out");
int n, k, x;
deque<pair<int, int>> q;
long long s;

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

        while (!q.empty() && q.front().second < i - k + 1)
            q.pop_front();

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

    out << s << '\n';
    return 0;
}