Pagini recente » Cod sursa (job #788009) | Cod sursa (job #1315619) | Cod sursa (job #2482645) | Istoria paginii runda/abc | Cod sursa (job #1046708)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
int v[5000002], n, k;
long long s;
unordered_map<int, int> m;
vector<int> h;
int main()
{
f>>n>>k;
for(int i=0; i<n; ++i)
f>>v[i];
for(int i=0; i<k; ++i) h.push_back(-v[i]), m[-v[i]]=i;
make_heap(h.begin(), h.end()); s+=-h.front();
for(int i=k; i<n; ++i){
h.push_back(-v[i]); m[-v[i]]=i;
push_heap(h.begin(), h.end());
while(m[h.front()]<=i-k){
pop_heap(h.begin(), h.end());
h.pop_back();
}
s+=-h.front();
//cout<<-h.front()<<' ';
}
g<<s;
// cout<<s;
return 0;
}