Pagini recente » Cod sursa (job #237324) | Cod sursa (job #1546550) | Cod sursa (job #451300) | Cod sursa (job #438892) | Cod sursa (job #2815438)
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
int arr[5000000];
int main() {
int arr_length, seq_length;
fin >> arr_length >> seq_length;
deque<int> best_pos_min;
long long sum = 0;
int current_value;
for (int i = 0; i < arr_length; ++i) {
fin >> current_value;
while (best_pos_min.size() && arr[best_pos_min.back()] > arr[i]) {
best_pos_min.pop_back();
}
best_pos_min.push_back(i);
if (i >= seq_length - 1) {
sum += arr[best_pos_min.front()];
}
if (best_pos_min.front() == i - seq_length + 1) {
best_pos_min.pop_front();
}
}
fout << sum;
return 0;
}