Cod sursa(job #295873)

Utilizator andreifirstCioara Andrei Ioan andreifirst Data 3 aprilie 2009 19:00:50
Problema Subsecventa de suma maxima Scor 95
Compilator fpc Status done
Runda Arhiva educationala Marime 0.59 kb
var n, i, x, sum, sump:longint;
    p1, p2, pp1, max:longint;
    f:text;

begin
sum:=0; sump:=0;
assign (f, 'ssm.in'); reset (f);

readln (f, n);
max:= -maxlongint;
for i := 1 to n do
  begin
  read (f, x);
  if x>max then max :=x;
  sump := sump+x;
  if sump<0 then
    begin
    sump:=0;
    pp1:=i+1;
    end
            else
    begin
    if sump >= sum then
      begin
      sum := sump;
      p1:=pp1;
      p2:=i;
      end;
    end;
  end;
close (f);

if sum=0 then sum:=max;
assign (f, 'ssm.out'); rewrite (f);
write (f, sum, ' ', p1, ' ', p2);
close (f);
end.