Cod sursa(job #2902041)

Utilizator elena1972Flueras Elena Ramona elena1972 Data 15 mai 2022 09:03:08
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

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

deque < pair <int, int> > D;
int n, k;
long long S;

int main()
{
    int x;
    fin >> n >> k;
    fin >> x;
    D.push_back({x, 1});
    for(int i = 2; i <= k; ++i)
        {
            fin >> x;
            while(!D.empty() && x < D.back().first)
                D.pop_back();

            D.push_back({x, i});
        }

    S += 1LL * D.front().first;

    for(int i = k + 1; i <= n; ++i)
    {
        fin >> x;

        if(i - D.front().second == k)
            D.pop_front();

        while(!D.empty() && x < D.back().first)
            D.pop_back();

        D.push_back({x, i});

        S += 1LL * D.front().first;
    }

    fout << S;
    return 0;
}