Pagini recente » Cod sursa (job #3148941) | Cod sursa (job #207544) | Cod sursa (job #615761) | Cod sursa (job #270874) | Cod sursa (job #2978767)
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
const int maxn = 5e6;
int v[maxn];
void printdeque(deque<int> a) {
while (!a.empty()){
cout << v[a.front()] << ' ';
a.pop_front();}
cout << '\n';
}
int main() {
int n, k;
fin >> n >> k;
for (int i = 0; i < n; i++)
fin >> v[i];
long long sum = 0;
deque<int> goolah;
goolah.push_back(0);
for (int i = 1; i < n; i++) {
while (!goolah.empty() && v[i] <= v[goolah.back()])
goolah.pop_back();
goolah.push_back(i);
while (goolah.back() - goolah.front() >= k)
goolah.pop_front();
// printdeque(goolah);
if (i >= k - 1) {
sum += 1LL*v[goolah.front()];
//cout << "adding " << v[goolah.front()] << '\n';
}
}
fout << sum;
return 0;
}