Pagini recente » Profil Corinne94s | Cod sursa (job #2816444) | Cod sursa (job #2580979) | Cod sursa (job #103956) | Cod sursa (job #3211231)
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
ifstream in("deque.in");
ofstream out("deque.out");
int main()
{
deque<int> a;
int v[5000001], k;
long long s = 0, n;
in >> n >> k;
for(int i = 0; i < n; i++)
in >> v[i];
for(int i = 0; i < n; i++)
{
while(!a.empty() && (v[i] < v[a.back()]))
{
a.pop_back();
}
a.push_back(i);
if(i == a.front() + k)
{
a.pop_front();
}
if(i >= k - 1)
{
s = s + v[a.front()];
}
}
out << s;
return 0;
}