Cod sursa(job #3213650)

Utilizator zavragiudavid dragoi zavragiu Data 13 martie 2024 12:30:44
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>
using namespace std;

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

int a[5000005], n;
deque<int> q;

int main()
{
  int i, k, x;
  long long s = 0;
  fin >> n >> k;
  for(i = 1; i <= n; i++)
    fin >> a[i];
  for(i = 1; i <= k; i++)
  {
    x = a[i];
    while(!q.empty() && x <= a[q.back()])
      q.pop_back();
    q.push_back(i);
  }
  s += a[q.front()];
  for(i = k + 1; i <= n; i++)
  {
    x = a[i];
    while(!q.empty() && x <= a[q.back()])
      q.pop_back();
    q.push_back(i);
    if(q.front() == i - k) q.pop_front();
    s += a[q.front()];
  }
  fout << s;
}