Pagini recente » Cod sursa (job #3135322) | Cod sursa (job #640800) | Cod sursa (job #2461886) | Cod sursa (job #2633777) | Cod sursa (job #2266592)
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
deque <pair <long long, int> > D;
int n,k;
long long rezolva()
{
fin>>n>>k;
long long s=0,x;
fin>>x;
D.push_back(make_pair(x,1));
for(int i=2; i<=n; i++)
{
fin>>x;
if(!D.empty())
{
if(x>D.back().first)
D.push_back(make_pair(x,i));
else
{
while(!D.empty()&&D.back().first>=x)
D.pop_back();
D.push_back(make_pair(x,i));
}
}
else D.push_back(make_pair(x, i));
if(i-k>=D.front().second)
D.pop_front();
if(i>=k)
s+=D.front().first;
}
return s;
}
int main()
{
fout<<rezolva();
return 0;
}