Cod sursa(job #2164713)

Utilizator PopeangaMihneaPopeanga Mihnea- Stefan PopeangaMihnea Data 13 martie 2018 09:22:48
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.49 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("deque.in");
ofstream fout("deque.out");
typedef long long LL;
deque<int>D;
LL x[5000001];
int n, k;
LL s;

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