Pagini recente » Cod sursa (job #460459) | Cod sursa (job #2320348) | Cod sursa (job #3253138) | Cod sursa (job #2730704) | Cod sursa (job #2254361)
#include <iostream>
#include <deque>
#include <fstream>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
deque <long long int> a, b;
long long int n, k;
void Test();
int main() {
f >> n >> k;
long long int x;
long long int Sum = 0;
//Test();
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;
}
void Test() {
a.push_back(1);
a.push_back(2);
cout << a.front() << " " << a.back() << " ";
a.pop_front();
cout << a.front();
}