Cod sursa(job #2866658)

Utilizator tomaionutIDorando tomaionut Data 9 martie 2022 21:28:37
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("deque.in");
ofstream fout("deque.out");
int n, k, a[5000005];
long long sol;
deque <int> d;
int main()
{
    int i, x;
    fin >> n >> k;
    for (i = 1; i <= n; i++)
        fin >> a[i];
    for (i = 1; i <= n; i++)
    {
        x = a[i];     
        while (!d.empty() and a[d.back()] >= x)
            d.pop_back();
        d.push_back(i);
        if (!d.empty() and i - k >= d.front())
            d.pop_front();
        if (i >= k)
            sol += a[d.front()];
    }
    fout << sol << "\n";

    return 0;
}