Cod sursa(job #3911)

Utilizator andrei_infoMirestean Andrei andrei_info Data 29 decembrie 2006 15:53:12
Problema Secventa 2 Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.07 kb
{infoarena - secventa 2 - arhiva}
const max = 50001;
var a,sum:array[1..max] of longint;
    n,k,dr,st,suma:longint;

procedure citire;
var i:longint;
begin
assign(input,'secv2.in'); reset(input);
readln(n,k);
for i:=1 to n do begin read(a[i]); a[i]:=a[i]; end;
close(input);
end;

procedure calc;
var i,smax,pozmax:longint;
begin
smax:=-maxint*10000;
sum[1]:=a[1];
for i:=2 to n do
    begin
    sum[i]:=sum[i-1]+a[i];
    if (sum[i] > smax) and (i >=k) then begin
                          smax:=sum[i];
                          pozmax:=i;
                          end;
    end;
dr:=pozmax;
sum[pozmax]:=a[pozmax];
smax:=-maxint*1000;
for i:=pozmax-1 downto 1 do
    begin
    sum[i]:=sum[i+1] + a[i];
    if (sum[i] > smax ) and (dr - i +1 >= k ) then
               begin
               smax:=sum[i];
               pozmax:=i;
               end;
    end;
st:=pozmax;
suma:=0;
for i:=st to dr do suma:=suma+a[i];
end;



begin
citire;
assign(output,'secv2.out'); rewrite(output);
calc;
writeln(st,' ',dr,' ',suma);

close(output);
end.