Cod sursa(job #570155)

Utilizator ion_calimanUAIC Ion Caliman ion_caliman Data 2 aprilie 2011 17:44:07
Problema Subsecventa de suma maxima Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 0.97 kb
var     a,sum:array[0..6000000] of longint;
        buf:array[1..100000000] of char;
        n,i,j,poz,min,t,p,s,x,y,best:longint;
        sir:string;
        f,g:text;

begin
  assign(f,'ssm.in');
  assign(g,'ssm.out');
  reset(f);
  settextbuf(f,buf);
  rewrite(g);
  readln(f,n);
  poz:=1;
  p:=1;
  while not eoln(f) do
    begin
      read(f,sir);
      for j:=1 to length(sir) do
        begin
          if sir[j]=' ' then begin a[poz]:=a[poz]*p; inc(poz); p:=1; end else
          if sir[j]='-' then p:=-1 else
            begin
              a[poz]:=a[poz]*10 + ord(sir[j])-ord('0');
            end;
        end;
    end;
  a[poz]:=a[poz]*p;
  for i:=1 to n do
    sum[i]:=sum[i-1]+a[i];

  s:=-1000000000;
  min:=0;
  x:=1;
  for i:=1 to n do
    begin
      best:=sum[i]-min;
      if min>sum[i] then begin t:=i+1; min:=sum[i]; end;
      if s<best then begin s:=best; y:=i; x:=t; end;
    end;

  write(g,s,' ',x,' ',y);
  close(g);
end.