Cod sursa(job #559073)
| Utilizator | Data | 17 martie 2011 16:43:12 | |
|---|---|---|---|
| Problema | Deque | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.56 kb |
#include<stdio.h>
int deq[5000100], t[5000100];
int main()
{
freopen("deque.in","r",stdin);
freopen("deque.out","w",stdout);
int n,i,down=0,up=0,k;
long long s=0;
scanf("%d%d",&n,&k);
t[0] = -2000000000;
deq[0]=0;
down = 1;
for(i=1;i<=n;i++)
{
scanf("%d",&t[i]);
while(t[i]<t[deq[up]]&&up>=down)
up--;
deq[++up]=i;
while(deq[down]<=i-k && up>=down)
down++;
if(i>=k)
s+=t[deq[down]];
}
printf("%lld\n",s);
return 0;
}
