Cod sursa(job #2662844)

Utilizator CRazvaN6Cutuliga Razvan CRazvaN6 Data 24 octombrie 2020 17:04:42
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
deque <int>q;
const int nmax=5e6;
int n,k;
long long s,a[nmax+2];
int main()
{
    f >> n >> k;
    for(int i = 1; i <= n; i++)
        f >> a[i];
    q.push_back(1);
    for(int i = 2 ; i <= n; i++)
    {
        while( !q.empty() && a[q.back()] >= a[i])
            q.pop_back();
        q.push_back(i);
        while(!q.empty() && q.front() <= i-k)
            q.pop_front();
        if(i>=k)
            s += a[q.front()];
    }
    g << s;
    return 0;
}