Cod sursa(job #1688732)

Utilizator TudorVersoiuVersoiu Tudor Sorin TudorVersoiu Data 13 aprilie 2016 18:17:21
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;
ifstream f("deque.in" );
ofstream g("deque.out");

int N, K, v[5000002]; long long S;
deque<int> heap;

void Inserare(int x) {
    while ( !heap.empty() && v[heap.back()] > v[x] ) heap.pop_back();

    heap.push_back( x );
}

int main() {
    f >> N >> K;

    for ( int i=1 ; i<=N ; i++ )
        f >> v[i];


    for ( int i=1 ; i<=N ; i++ ) {
        Inserare( i );


        if ( heap.front() <= i-K )        heap.pop_front();
        if ( i >= K )              S += v[heap.    front()];
    }

    g << S;
}