Cod sursa(job #2777830)

Utilizator Maftei_TudorMaftei Tudor Maftei_Tudor Data 25 septembrie 2021 10:34:11
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <fstream>
#include <deque>

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

struct nr
{
    int val, poz;
};

deque<nr> deq;

int n, k;
long long s;
nr x;

int main()
{
    fin>>n>>k;
    for(int i=1; i<=n; i++)
    {
        fin>>x.val;
        x.poz=i;
        while(!deq.empty() && deq.front().poz<i-k+1) deq.pop_front();
        while(!deq.empty() && deq.back().val>x.val) deq.pop_back();
        deq.push_back(x);
        if(i>=k) s+=deq.front().val;
    }
    fout<<s;
    return 0;
}