Cod sursa(job #2575184)

Utilizator chriss_b_001Cristian Benghe chriss_b_001 Data 6 martie 2020 12:00:18
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <iostream>
#include <deque>
#include <fstream>

using namespace std;

deque <pair<int, int> > Q;

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

int main()
{
    long long N, K, S = 0;
    pair<int, int> x;
    f >> N >> K;
    for(int i = 1; i <= N; i++)
    {
        f >> x.first;
        x.second = i;
        while(!Q.empty() && Q.back() >= x)Q.pop_back();
        Q.push_back(x);

        if(K <= i)S += Q.front().first;
        if(K <= i)
            if(i - K + 1 == Q.front().second)Q.pop_front();
    }
    g << S;
    return 0;
}