Cod sursa(job #2052932)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 31 octombrie 2017 10:47:51
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;
ifstream in ("deque.in");
ofstream out ("deque.out");
int const nmax = 5000000;
int v[1 + nmax];
deque<int> h;

int main()
{
  int n , k;
  in>>n>>k;
  long long sum = 0;
  for(int i = 1 ; i <= n ;i++){
    in>>v[i];
    while(0 < h.size() && (v[i] <= v[h.back()] || h.back() <= i - k)){
      h.pop_back();
    }

    h.push_back(i);
    while(h.front() <= i - k){
      h.pop_front();
    }
    if(k <= i){
      sum += v[h.front()];
    }
  }
  out<<sum;
  return 0;
}