Pagini recente » Cod sursa (job #3330302) | Cod sursa (job #3336283) | Cod sursa (job #3334490) | Cod sursa (job #3305950) | Cod sursa (job #3301500)
// https://infoarena.ro/problema/deque
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <functional>
#include <deque>
using namespace std;
ifstream f("input.txt");
ofstream g("output.txt");
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});
pair<int, int> topMin = mind.top();
while (mind.top().second < i - k + 1) {
mind.pop();
}
if (!mind.empty() && i >= k - 1) {ans += (int)mind.top().first;}
}
cout << ans;
return 0;
}