Pagini recente » Cod sursa (job #1452600) | Cod sursa (job #1156044) | Cod sursa (job #3222890) | Cod sursa (job #1134019) | Cod sursa (job #1990455)
#include <iostream>
#include <fstream>
#include <list>
struct Pair {
int first, second;
};
int main() {
std::ifstream fileIn("deque.in");
std::ofstream fileOut("deque.out");
std::list<Pair> myQ;
int nV, nK;
fileIn >> nV >> nK;
long long sum(0);
for (int i(1); i <= nV; i++) {
Pair aux;
fileIn >> aux.second;
aux.first = i;
while (!myQ.empty() && myQ.front().first <= i - nK) {
myQ.pop_front();
}
while (!myQ.empty() && myQ.back().second >= aux.second) {
myQ.pop_back();
}
myQ.push_back(aux);
if (i >= nK) {
sum += myQ.front().second;
}
}
fileOut << sum;
fileIn.close();
fileOut.close();
return 0;
}