Cod sursa(job #569450)

Utilizator ion_calimanUAIC Ion Caliman ion_caliman Data 1 aprilie 2011 15:00:45
Problema Subsecventa de suma maxima Scor 5
Compilator fpc Status done
Runda Arhiva educationala Marime 1.26 kb
var     a:array[1..6000000] of longint;
        v:array[1..3,1..6000000] of longint;
        n,i,j,k,t,poz,s,s1,s2,s3,x,y,z:longint;

        f,g:text;

begin
  assign(f,'ssm.in');
  assign(g,'ssm.out');
  reset(f);
  rewrite(g);
  readln(f,n);
  for i:=1 to n do
    read(f,a[i]);
  i:=1;
  poz:=1;
  while a[i]<=0 do inc(i);
  s:=0;
  s3:=0;
  t:=i;
  while i<=n do
    begin
      j:=i;
      s1:=0;
      while a[j]>0 do
        begin
          s1:=s1+a[j];
          inc(j);
        end;
      k:=j;
      s2:=0;
      while a[k]<0 do
        begin
          s2:=s2+a[k];
          inc(k);
        end;
     if s2<0 then begin
     if s1+s2>=0 then
        begin
          s3:=s3+s1+s2;
          i:=k;
        end else
        begin
          s3:=s3+s1;
          if s3>s then
            begin
              s:=s3;
              x:=t;
              y:=j-1;
            end;
          t:=k;
          i:=k;
          s3:=0;
        end; end else
        begin
          s3:=s3+s1;
          if s3>s then
            begin
              s:=s3;
              x:=t;
              y:=j-1;
            end;
          t:=k;
          i:=k;
          s3:=0;
        end;
    end;
  write(g,s,' ',x,' ',y);
  close(g);
end.