Cod sursa(job #2732458)

Utilizator anaop32Oprea Ana-Maria anaop32 Data 28 martie 2021 23:02:31
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream f ("deque.in");
ofstream g ("deque.out");
int dq[5000001], aux[5000001];

int main(){
    long long n, k;
    f >> n >> k;
    long long suma = 0;
    int frontq = 1, backq = 0;
    for (int i = 1; i <= n; i++)
        f>>aux[i];

    for (int i = 1; i <= n; i++){
        while (frontq <= backq && aux[i] <= aux[dq[backq]])
            backq--;
        dq[++backq] = i;
        if (dq[frontq] == i- k)
            frontq ++;
        if (i >= k)
            suma+= aux[dq[frontq]];

    }
    g << suma;
    return 0;
}