Cod sursa(job #876906)

Utilizator Alexxino7Alexandru Popescu Alexxino7 Data 12 februarie 2013 12:01:52
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include<fstream>
#include<iostream>
using namespace std;
#define Max 5000005

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

int L=1,R=0,N,K,V[Max],D[Max];
long long Sol;

int main(){

    fin>>N>>K;
    for(int i=1;i<=N;i++){
        fin>>V[i];
    }

    for(int i=1;i<=N;i++){
        D[++R]=i;
        if(i-K>=D[L]){
            L++;
        }
        while(L<R && V[D[R]]<=V[D[R-1]]){
            D[R-1]=D[R],R--;
        }
        if(i>=K)
        Sol+=V[D[L]];
    }

    fout<<Sol<<"\n";

    return 0;
}