Cod sursa(job #3205931)
| Utilizator | Data | 21 februarie 2024 00:58:09 | |
|---|---|---|---|
| Problema | Deque | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.63 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in("deque.in");
ofstream out("deque.out");
int n, k, a[5000005];
int main()
{
in >> n >> k;
for (int i = 1; i <= n; i++) {
in >> a[i];
}
deque<pair<int, int>> q;
long long sum = 0;
for (int i = 1; i <= n; i++) {
while(!q.empty() && q.back().first > a[i])
q.pop_back();
q.push_back({a[i], i});
while (!q.empty() && q.front().second < i - k + 1)
q.pop_front();
if (i >= k) {
sum += q.front().first;
}
}
out << sum << '\n';
return 0;
}
