Cod sursa(job #2999595)

Utilizator OneShotStefBurlacenco Bayer Stefan OneShotStef Data 11 martie 2023 10:45:50
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, a[5000001], q[5000001], k;
long long s = 0;
int uq = -1, pq = 0;

void citire()
{
    fin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        fin >> a[i];
    }
}

int main()
{
    citire();
    for (int i = 1; i <= n; i++)
    {
        while (pq <= uq && a[i] <= a[q[uq]])
        {
            uq--;
        }
        q[++uq] = i;
        if (q[pq] <= i - k)pq++;
        if (i >= k)
            s+=a[q[pq]];
    }
    fout << s;
}