Pagini recente » Cod sursa (job #1969493) | Cod sursa (job #1603127) | Cod sursa (job #3244896) | Cod sursa (job #2541117) | Cod sursa (job #237012)
Cod sursa(job #237012)
#include<stdio.h>
struct ddd{
int val,poz;
}deq[5000000];
int main(){
freopen("deque.in","r",stdin);
freopen("deque.out","w",stdout);
int n,k,i,st,dr,x;
long long sol=0;
scanf("%d%d",&n,&k);
st=dr=0;
for(i=1;i<=n;++i){
scanf("%d",&x);
if(deq[st].poz<=i-k)
++st;
while(deq[dr].val>x && dr>=st)
--dr;
deq[++dr].val=x;
deq[dr].poz=i;
if(i>=k)
sol+=deq[st].val;
}
printf("%lld\n",sol);
fclose(stdin);
fclose(stdout);
return 0;
}