Cod sursa(job #3002511)

Utilizator tomaionutIDorando tomaionut Data 14 martie 2023 20:45:27
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("deque.in");
ofstream fout("deque.out");

int a[5000005], n, k;
long long sol = 0;
deque <int> d;

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

    return 0;
}