Cod sursa(job #743331)

Utilizator Buzu_Tudor_RoCont vechi Buzu_Tudor_Ro Data 3 mai 2012 22:36:02
Problema Subsecventa de suma maxima Scor 60
Compilator fpc Status done
Runda Arhiva educationala Marime 0.8 kb
Program p1;
var fi,fo : text;
    i,n,j1,j2,max : longint;
    a,c:array[0..600005] of longint;

begin
    assign(fi,'ssm.in'); reset(fi); readln(fi,n);
    assign(fo,'ssm.out'); rewrite(fo);
    read(fi,a[1]); max:=a[1]; j1:=1; j2:=1;  c[1]:=a[1];
    for i:=2 to n do begin
                     read(fi,a[i]);
                     if  c[i-1]+a[i]>=a[i] then c[i]:=c[i-1]+a[i]
                                           else c[i]:=a[i];
                     if c[i]>max then begin max:=c[i]; j2:=i; end;
                     end;
    j1:=j2;
    write(fo,max,' ');
    while max<>0 do begin
                    max:=max-a[j1];
                    j1:=j1-1;
                    end;
    if a[j1]<>0 then j1:=j1+1;
    if j1>j2 then j1:=j2;
    write(fo,j1,' ',j2);
    close(fi); close(fo);
end.