Cod sursa(job #2472928)

Utilizator sygAndreiIonitaIonita Andrei sygAndreiIonita Data 13 octombrie 2019 11:12:55
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.51 kb
#include <fstream>
#include <deque>

using namespace std;

deque < pair <int,int> > d;

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

int main()
{
    int n,k,i,x;
    long long sum=0;
    in>>n>>k;
    for (i=1;i<=n;i++)
    {
      in>>x;
      while (!d.empty()&&x<=d.back().first)
            d.pop_back();
      d.push_back(make_pair(x,i));
      if (d.front().second==i-k)
        d.pop_front();
      if (i>=k)
        sum+=d.front().first;
    }
    out<<sum;
    return 0;
}