Pagini recente » Cod sursa (job #1307069) | Rating Ioana Stratan (rstr) | Cod sursa (job #1704083) | Cod sursa (job #359247) | Cod sursa (job #1982271)
#include <iostream>
#include <fstream>
using namespace std;
ifstream cin("deque.in");
ofstream cout("deque.out");
const int N=5000001;
int st=1, dr , d[N], v[N],k,n;
int main()
{
long long i,S=0;
cin>>n>>k;
for(i=1; i<=n; i++)
cin>>v[i];
for(i=1; i<=n; i++)
{
while (st <= dr && v[i] <= v[d[dr]])
{
dr--;
}
d[++dr] = i;
if (d[st] == i - k)
{
++st;
}
if(i>=k)
S=S+v[d[st]];
}
cout<<S;
return 0;
}