Cod sursa(job #3126991)

Utilizator florinilie324Ilie Florin Alexandru florinilie324 Data 7 mai 2023 04:00:15
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <deque>
#include <vector>
#include <fstream>

using namespace std;

int main() {
    ifstream fin("deque.in");
    ofstream fout("deque.out");
    int N, K;
    fin >> N >> K;
    vector<int> A(N);

    for (int i = 0; i < N; i++) {
        fin >> A[i];
    }

    deque<int> dq;
    long long sum = 0;

    for (int i = 0; i < N; i++) {
        while (!dq.empty() && A[dq.back()] >= A[i]) {
            dq.pop_back();
        }

        dq.push_back(i);

        if (i >= K - 1) {
            sum += A[dq.front()];
        }

        if (!dq.empty() && dq.front() == i - K + 1) {
            dq.pop_front();
        }
    }

    fout << sum << endl;

    return 0;
}