Cod sursa(job #2036268)

Utilizator ciocirlanrCiocirlan Robert ciocirlanr Data 10 octombrie 2017 16:02:48
Problema Deque Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <fstream>
#include <deque>
using namespace std;
ifstream in("deque.in");
ofstream out("deque.out");

deque<int> B;
int a[5000001];
int N,k;
long long S;

int main(){

    in >> N >> k;
    for(int i = 1; i <= N; ++i){
        in >> a[i];
        while(!B.empty() and a[B.front()]>a[i])
            B.pop_front();

        B.push_front(i);

        if(!B.empty() and i-B.back()+1>=k)
            B.pop_back();

        if(i>=k)   S+=a[B.back()];
    }
    out << S;

    return 0;
}