Pagini recente » Cod sursa (job #300501) | Cod sursa (job #2668500) | Cod sursa (job #365916) | Cod sursa (job #2128955) | Cod sursa (job #625905)
Cod sursa(job #625905)
#include <stdio.h>
int N,K,A[5000000],Deque[5000000],start=1,end=0;
long long suma;
//void deque()
//{
//
//for(int i=1;i<=N;i++)
//{
// while(start<=end && A[i]<=A[Deque[end]])
// end--;
//
// Deque[++end]=i;
// if(Deque[end]-Deque[start]==K)
// start++;
//
// if(i>=K)
// suma+=A[Deque[start]];
//
//
//}
//
//}
int main()
{
FILE *R,*W;
R=fopen("deque.in","r");
W=fopen("deque.out","w");
fscanf(R,"%d",&N);
fscanf(R,"%d",&K);
//for(int i=1;i<=N;i++)
for(int i=1;i<=N;i++)
{
fscanf(R,"%d",&A[i]);
while(start<=end && A[i]<=A[Deque[end]])
end--;
Deque[++end]=i;
if(Deque[end]-Deque[start]==K)
start++;
if(i>=K)
suma+=A[Deque[start]];
}
//deque();
fprintf(W,"%lld",suma);
fclose(R);
fclose(W);
return 0;
}