Cod sursa(job #323989)

Utilizator DeadEyeNaiba Mihai Lucian DeadEye Data 14 iunie 2009 12:23:54
Problema Deque Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.79 kb
var f,g:text;
    n,i,x,k,st,dr:longint; s:int64;
    a,o:array[1..5000000] of longint;
begin
    assign(f,'deque.in'); reset(f);
    assign(g,'deque.out'); rewrite(g);
    readln(f,n,k); s:=0;
    st:=1; dr:=0;
    for i:=1 to n do
         readln(a[i]);
    for i:=1 to k-1 do
       begin
          //readln(f,a[i]);
          while (st<=dr) and (a[i]<=a[o[dr]]) do
             dec(dr);
          inc(dr); o[dr]:=i;
          if o[st]+k-i=0
             then inc(st)
       end;
    for i:=k to n do
       begin
          //readln(f,a[i]);
          while (st<=dr) and (a[i]<=a[o[dr]]) do
             dec(dr);
          inc(dr); o[dr]:=i;
          if o[st]+k-i=0
             then inc(st);
          s:=s+a[o[st]];
       end;
    writeln(g,s);
    close(f); close(g);
end.