Cod sursa(job #408518)

Utilizator hungntnktpHungntnktp hungntnktp Data 3 martie 2010 06:01:45
Problema Deque Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.99 kb
{$M 64000000,0}
{$H-,I+,Q+,R+,S+}
{La Hoang
ngay 3-3-2010}
const
   TFI  = 'deque.in';
   TFO  = 'deque.out';
   MaxN = 5000000;
var
   fi, fo: text;
   Res: int64;
   n, first, last, i, k: longint;
   Q, A: array[1..MaxN] of longint;
   (*-----------------------------------*)
   procedure Push(i: longint);
   begin
      While (first <= last) and (A[Q[last]] > A[i]) do dec(last);
      inc(last);
      Q[last] := i;
      While (first <= last) and (Q[first] < i - k + 1) do inc(first);
   end;
   (*-----------------------------------*)
begin
   Assign(fi, TFI); Reset(fi);
   Assign(fo, TFO); Rewrite(Fo);
   last := 0; first := 1;
   Readln(fi, n);
   Readln(fi, k);
   for i := 1 to k - 1 do
      begin
         Readln(fi, A[i]);
         inc(last);
         Q[last] := i;
      end;
   for i := k to n do
      begin
         Readln(fi, A[i]);
         Push(i);
         inc(Res, A[Q[first]]);
      end;
   Writeln(fo, Res);
   Close(fo);
   Close(fi);
end.