Pagini recente » Cod sursa (job #2883298) | Cod sursa (job #857193) | Cod sursa (job #2924261) | Cod sursa (job #1832007) | Cod sursa (job #932406)
Cod sursa(job #932406)
var f,g:text;
bufin,bufout:array[1..65000]of byte;
ind,c:array[-1..5000010] of longint;
i,n,k:longint;
l,pr,sum,x:int64;
begin
assign(f,'deque.in');
assign(g,'deque.out');
reset(f);
rewrite(g);
settextbuf(f,bufin);
settextbuf(g,bufout);
readln(f,n,k);
pr:=1;
l:=0;
for i:=1 to k do begin
read(f,x);
inc(l);
while (x<c[l-1]) and (l>1) do dec(l);
c[l]:=x;
ind[l]:=i;
end;
sum:=c[1];
for i:=k+1 to n do begin
read(f,x);
if ind[pr]=i-k then inc(pr);
inc(l);
while (x<c[l-1]) and (l>pr) do dec(l);
c[l]:=x;
ind[l]:=i;
sum:=sum+c[pr];
end;
writeln(g,sum);
close(g);
end.