Pagini recente » Cod sursa (job #3310124) | Cod sursa (job #775466) | Monitorul de evaluare | Cod sursa (job #3309060) | Cod sursa (job #3301502)
// https://infoarena.ro/problema/deque
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <functional>
#include <deque>
using namespace std;
ifstream f("grader_test10.in");
ofstream g("deque.out");
struct cmp {
bool operator()(const pair<int, int>& a, const pair<int, int>& b) {
return a.first > b.first;
}
};
int main() {
int n, k;
f >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
f >> a[i];
}
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> mind;
int ans = 0;
for (int i = 0; i < n; i++) {
mind.push({a[i], i});
while (mind.top().second < i - k + 1) {
mind.pop();
}
if (!mind.empty() && i >= k - 1) {ans += (int)mind.top().first;}
}
g << ans;
g.close();
return 0;
}