Cod sursa(job #846287)

Utilizator brainwashed20Alexandru Gherghe brainwashed20 Data 1 ianuarie 2013 20:05:47
Problema Deque Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include<fstream>
#include<queue>

using namespace std;

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

int N, K, val;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > PQ;
long long rez;

int main() {

    int i;

    f>>N>>K;

    for(i=1; i<=N; ++i) {
        f>>val;
        PQ.push(make_pair(val, i));
        while(!PQ.empty() && PQ.top().second <= i-K)
            PQ.pop();
        if(i>=K)
            rez+=PQ.top().first;
    }

    g<<rez<<"\n";

    f.close(); g.close();

    return 0;
}