Cod sursa(job #2751707)

Utilizator MihneaCadar101Cadar Mihnea MihneaCadar101 Data 15 mai 2021 16:57:45
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
int n, k;

deque <int> dq;
vector <int> v;

int main()
{
    fin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        int x;
        fin >> x;
        v.push_back(x);
    }

    long long s = 0;
    for (int i = 0; i < k; ++i) {
        while (!dq.empty() && v[i] <= v[dq.back()]) dq.pop_back();

        dq.push_back(i);
    }

    s += v[dq.front()];
    for (int i = k; i < n; ++i) {
        if (dq.front() < i - k + 1) dq.pop_front();

        while (!dq.empty() && v[i] <= v[dq.back()]) dq.pop_back();

        dq.push_back(i);
        s += v[dq.front()];
    }

    fout << s;
    return 0;
}