Cod sursa(job #2264831)

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

using namespace std;

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

deque <pair <long long, int > > deck;

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

    return 0;
}