Cod sursa(job #3246649)

Utilizator TeddyDinutaDinuta Eduard Stefan TeddyDinuta Data 3 octombrie 2024 21:17:48
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;

int main() {
    in >> n >> k;
    deque<pair<int, int>> q;
    long long sum = 0;

    for (int i = 0; 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)
            q.pop_front();

        if (i >= k - 1) {
            sum += q.front().first;
        }

    }

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