Pagini recente » Cod sursa (job #1102924) | Cod sursa (job #680318) | Cod sursa (job #1111674) | Cod sursa (job #1066995) | Cod sursa (job #2674907)
#include <fstream>
#include <deque>
#include <utility>
using namespace std;
ifstream cin("deque.in");
ofstream cout("deque.out");
deque< pair <int,int> >d;
int main()
{
long long n,k,x,i,sum=0;
cin>>n>>k;
for(i=1; i<=n; i++)
{
cin>>x;
while(x<=d.front().first&&d.size()>0)
{
d.pop_front();
}
d.push_front(make_pair(x,i));
if(i>=k)
sum=sum+d.back().first;
if(d.back().second==i-k+1)
{
d.pop_back();
}
}
cout<<sum;
return 0;
}