Cod sursa(job #2620388)

Utilizator AlexVulpoiuAlexandru Vulpoiu AlexVulpoiu Data 28 mai 2020 20:16:16
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>

using namespace std;

ifstream f("deque.in");
ofstream g("deque.out");

int n, k, i, x, p, u, a[5000005], b[5000005];
long long int s;

int main()
{
    s = 0;

    f >> n >> k;
    p = u = 0;
    for(i = 1; i <= n; i++)
    {
        f >> x;

        while(p <= u && a[u] > x)
            u--;
        u++;
        a[u] = x;
        b[u] = i;
        while(p <= u && b[p] <= i - k)
            p++;
        if(i >= k)
            s += 1LL * a[p];
    }

    g << s << "\n";

    f.close();
    g.close();

    return 0;
}