Pagini recente » Cod sursa (job #1743900) | Cod sursa (job #1178432) | Cod sursa (job #2601468) | Cod sursa (job #853469) | Cod sursa (job #1255444)
#include<cstdio>
#include<deque>
using namespace std;
int n,k,i,a[5000004];
long long s;
deque<int>c;
int main()
{
freopen("deque.in","r",stdin);
freopen("deque.out","w",stdout);
scanf("%d %d",&n,&k);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
while(!c.empty()&&a[c.back()]>=a[i])c.pop_back();
c.push_back(i);
if(c.front()<=i-k)c.pop_front();
if(i>=k)s=s+a[c.front()];
}
printf("%lld",s);
return 0;
}