Pagini recente » Cod sursa (job #811577) | Cod sursa (job #2979836) | Cod sursa (job #2674379) | Cod sursa (job #2741563) | Cod sursa (job #2902041)
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
deque < pair <int, int> > D;
int n, k;
long long S;
int main()
{
int x;
fin >> n >> k;
fin >> x;
D.push_back({x, 1});
for(int i = 2; i <= k; ++i)
{
fin >> x;
while(!D.empty() && x < D.back().first)
D.pop_back();
D.push_back({x, i});
}
S += 1LL * D.front().first;
for(int i = k + 1; i <= n; ++i)
{
fin >> x;
if(i - D.front().second == k)
D.pop_front();
while(!D.empty() && x < D.back().first)
D.pop_back();
D.push_back({x, i});
S += 1LL * D.front().first;
}
fout << S;
return 0;
}