Pagini recente » Cod sursa (job #2000312) | Cod sursa (job #2589924) | Cod sursa (job #146818) | Cod sursa (job #652047) | Cod sursa (job #2258278)
#include <iostream>
#include <deque>
#include <fstream>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
deque <long int> a;
deque <int> b;
int n, k;
int main() {
f >> n >> k;
long long int x;
long long int Sum = 0;
for (int i = 1; i <= n; ++ i) {
f >> x;
bool Condition = true;
while (a.empty() == 0 && Condition) {
if (a.back() > x) {
a.pop_back();
b.pop_back();
} else {
Condition = false;
}
}
a.push_back(x);
b.push_back(i);
if (i >= k) {
long long int pos = b.front();
long long int val = a.front();
//cout << val << " ";
Sum += val;
if (pos + k == i + 1 ) {
b.pop_front();
a.pop_front();
}
}
}
g << Sum;
return 0;
}