Pagini recente » Cod sursa (job #3199726) | Cod sursa (job #2132151) | Cod sursa (job #789321) | Cod sursa (job #2797447) | Cod sursa (job #2964291)
#include <fstream>
#include <deque>
using namespace std;
ifstream cin("deque.in");
ofstream cout("deque.out");
const int dim = 5e6 + 5;
deque<int> q;
int v[dim];
int main() {
int n, k, i, j, x, s = 0;
cin >> n >> k;
for (i = 1; i <= n; ++i)
cin >> v[i];
for (i = 1; i <= n; ++i) {
while (!q.empty() && v[q.back()] >= v[i])
q.pop_back();
q.push_back(i);
while (!q.empty() && q.front() <= i - k)
q.pop_front();
if (i >= k)
s += v[q.front()];
}
cout << s;
return 0;
}