Pagini recente » Cod sursa (job #1142974) | Cod sursa (job #1853703) | Cod sursa (job #3313510) | Cod sursa (job #1424063) | Cod sursa (job #3334975)
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ifstream cin{"deque.in"};
ofstream cout{"deque.out"};
int N, K;
cin >> N >> K;
deque<pair<int, int>> q;
long long sum = 0;
for (int i = 1; i <= N; i++) {
int x;
cin >> x;
while (!q.empty() && x <= q.back().first) {
q.pop_back();
}
while (!q.empty() && q.front().second <= i - K) {
q.pop_front();
}
q.push_back({x, i});
if (K <= i) {
sum += q.front().first;
}
}
cout << sum << '\n';
return 0;
}
/*
-7
9
2
4
-1
5
6
7
1
-7 + 2 -1 -1 -1 5 1
3^2 + (1 + 3) ^ 2 + (5 + 1 + 3) ^ 2 + (5 + 1) ^ 2 + 1^2 + 5^2 =
9 + 16 + 81 + 36 + 1 + 25 = 168
3 + 1 + 3 + 5 + 1 + 3 + 5 + 1 + 1 + 5
*/