Cod sursa(job #2264697)

Utilizator RaresV227Virjoghe Rares Constantin RaresV227 Data 20 octombrie 2018 11:14:02
Problema Deque Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

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

deque <pair <int, int > > e;

int main()
{
    int n, k, x, s=0;
    f >> n >> k;
    f >> x;
    e.push_back({x, 1});
    for(int i=2; i<=n; i++)
    {
        f >> x;
        if (x > e.back().first)
            e.push_back({x, i});
        else
        {
            while(x <= e.back().first)
                e.pop_back();
            e.push_back({x, i});
        }
        if(i - e.front().second == k)
            e.pop_front();
        if(i >= k)
            s += e.front().first;
    }
    g << s;

    return 0;
}