Cod sursa(job #2727841)

Utilizator bestman4111Tiberiu Niculae bestman4111 Data 22 martie 2021 15:52:17
Problema Deque Scor 25
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <fstream>
using namespace std;

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

int N, K;
int sum = 0;

int main()
{
    cit>>N>>K;
    int deque[N], poz[N];
    int back = -1, front = 0;
    for(int i=0; i<N; i++)
    {
        cit>>deque[i];
        while((front<=back) && (deque[i]<=deque[back]))
            back--;
        deque[++back] = deque[i];
        poz[back] = i;
        if(poz[front] == i-K)
            front++;
        if(i+1 >= K)
            sum += deque[front];
    }
    afis<<sum;
    return 0;
}