Cod sursa(job #2887456)

Utilizator andlftLefter Andrei andlft Data 9 aprilie 2022 17:46:36
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <iostream>
#include <fstream>


using namespace std;

int deq[5000005];
int values[5000005];

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

int n, k;
fin >> n >> k;


long long suma = 0;
int st, dr;

st = 0;
dr = 0;

for(int i = 0; i < n; i++)
{
    fin>>values[i];
    while(st <= dr && values[i] < values[deq[dr]] )
    {
        dr--;
    }
    dr++;
    deq[dr] = i;


    while(deq[st]< deq[dr] - k + 1)
    {
        st++;
    }

    if(i > k - 2)
    {
        suma += values[deq[st]];
    }
}


fout<<suma;


fin.close();
fout.close();
    return 0;
}