Pagini recente » Cod sursa (job #709435) | Cod sursa (job #857870) | Cod sursa (job #2563588) | Cod sursa (job #829072) | Cod sursa (job #2575122)
#include <iostream>
#include <deque>
#include <fstream>
using namespace std;
deque <pair<int, int> > Q;
ifstream f("deque.in");
ofstream g("deque.out");
int main()
{
int N, K, S = 0;
pair<int, int> x;
f >> N >> K;
for(int i = 1; i <= K; i++)
{
f >> x.first;
x.second = i;
while(!Q.empty() && Q.back().first >= x.first)
Q.pop_back();
Q.push_back(x);
}
S += Q.front().first;
Q.pop_front();
for(int i = K + 1; i <= N; i++)
{
f >> x.first;
x.second = i;
while(!Q.empty() && Q.front().second <= i - K)Q.pop_front();
while(!Q.empty() && Q.back().first >= x.first)
Q.pop_back();
Q.push_back(x);
S += Q.front().first;
}
g << S;
return 0;
}