Pagini recente » Cod sursa (job #1974171) | Cod sursa (job #234605) | tema | Cod sursa (job #485193) | Cod sursa (job #1982903)
#include <iostream>
#include <fstream>
using namespace std;
const int N=5e6;
int dq[N+1],v[N+1];
int main()
{
ifstream cin("deque.in");
ofstream cout("deque.out");
int p=0,u=0;
int k,i,n;
long long ans=0;
cin>>n>>k;
for(i=1;i<=n;i++)
cin>>v[i];
for(i=1;i<=n;i++)
{
while(p && u && p<=u && i-dq[p]>=k)
++p;
while(p && u && p<=u && v[dq[u]]>=v[i])
--u;
dq[++u]=i;
if(p ==0)
p=1;
if(i>=k) {
ans +=v[dq[p]];
}
}
cout<<ans;
return 0;
}