Cod sursa(job #570106)

Utilizator ion_calimanUAIC Ion Caliman ion_caliman Data 2 aprilie 2011 16:35:31
Problema Subsecventa de suma maxima Scor 35
Compilator fpc Status done
Runda Arhiva educationala Marime 1.63 kb
var     a,b:array[0..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
    begin
      read(f,a[i]);
      b[i]:=b[i-1]+a[i];
    end;
  s:=-10000000;
  for i:=1 to n do
    begin
      for j:=0 to i-1 do
      if b[i]-b[j]>s then
        begin
          s:=b[i]-b[j];
          x:=j+1;
          y:=i;
        end else
      if b[i]-b[j]=s then
      if i-j<y-x then
        begin
          x:=j+1;
          y:=i;
        end;
    end;
{  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.