Cod sursa(job #2502234)

Utilizator hurjui12AlexandruHurjui Alexandru-Mihai hurjui12Alexandru Data 30 noiembrie 2019 15:22:50
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
#include <queue>
using namespace std;

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

int main()
{
    long long s;
    deque <pair <int, int>> d;
    int n, k, i, x;
    fin >> n >> k;
    for (i = 1; i<=k; i++)
    {
        fin >> x;
        while (d.empty() == 0 && d.front().first > x)
            d.pop_front();
        d.push_front({x, i});
    }
    s = d.back().first;
    for (i = k+1; i<=n; i++)
    {
        fin >> x;
        if (i - d.back().second == k)
            d.pop_back();
        while (d.empty() == 0 && d.front().first > x)
            d.pop_front();
        d.push_front({x, i});
        //fout << d.back().first << ' ';
        s = s + d.back().first;
    }
    fout << s;
    return 0;
}