Pagini recente » Cod sursa (job #983904) | Cod sursa (job #2652073) | Cod sursa (job #2310204) | Cod sursa (job #356758) | Cod sursa (job #2650778)
#include <bits/stdc++.h>
using namespace std;
int main () {
freopen("deque.in", "r", stdin);
freopen("deque.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, k;
fin >> n >> k;
deque <pair <int, int>> DQ;
long long ans = 0;
int i, x;
for (i = 1; i <= n; i++) {
fin >> x;
while (!DQ.empty() && DQ.back().second >= x)
DQ.pop_back();
DQ.emplace_back(i, x);
if (DQ.front().first == i - k)
DQ.pop_front();
if (i >= k)
ans += DQ.front().second;
}
fout << ans;
return 0;
}