Cod sursa(job #2264364)

Utilizator MDiana15Diana M MDiana15 Data 20 octombrie 2018 08:46:37
Problema Deque Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.46 kb
#include <fstream>
#include <deque>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
deque<int>d;
int n,i,k,v[5000005];
long long s;
int main()
{
   f>>n>>k;
   for(i=1;i<=n;i++)f>>v[i];
   for(i=1;i<=n;i++)
   {
       while(!d.empty()&&v[d.back()]>=v[i])
       {
           d.pop_back();
       }
       d.push_back(i);
       if(i-d.front()==k)d.pop_front();
       if(i>=k)s=s+v[d.front()];
   }
   g<<s;
    return 0;
}