Cod sursa(job #2382855)

Utilizator ezioconnorVlad - Gabriel Iftimescu ezioconnor Data 18 martie 2019 18:48:40
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.52 kb
#include <fstream>

using namespace std;

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

int a[5000001], d[5000001];
int st = 1, dr;
long long s;

int main()
{
    int n, k;
    in >> n >> k;
    for (int i = 1; i <= n; ++i)
        in >> a[i];
    for (int i = 1; i <= n; ++i)
    {
        while (st <= dr && a[i] <= a[d[dr]])
            --dr;
        d[++dr] = i;
        if (d[st] == i - k)
            ++st;
        if (i >= k)
            s += a[d[st]];
    }
    out << s;
    return 0;
}