Cod sursa(job #604818)

Utilizator andreii1Ilie Andrei andreii1 Data 25 iulie 2011 15:12:12
Problema Subsecventa de suma maxima Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 1.2 kb
var n,max,st,dr,i,aux,min,ciu:longint;
    a,q:array[0..6000000] of longint;
    f,g:text;
    bool:boolean;


begin
assign(f,'ssm.in');
reset(f);
assign(g,'ssm.out');
rewrite(g);
read(f,n);
bool:=true;
ciu:=-maxlongint;
for i:=1 to n do
        begin
        read(f,a[i]);
        if a[i]>0 then bool:=false;
        q[i]:=q[i-1]+a[i];
        end;
if bool then
        begin
        for i:=1 to n do
        if a[i]>ciu then
                begin
                ciu:=a[i];
                st:=i;
                dr:=i;
                end;
write(g,ciu,' ',st,' ',dr);
close(f);
close(g);
        end
else
begin
max:=a[1];
st:=1;
dr:=1;
min:=q[1];
for i:=2 to n do
        begin
        if q[i]<min then
                begin
                aux:=i;
                min:=q[i];
                end;
        if q[i]-min>max then
                begin
                max:=q[i]-min;
                st:=aux+1;
                dr:=i;
                end;
        if q[i]>max then
                begin
                max:=q[i];
                st:=1;
                dr:=i;
                end;
        end;
write(g,max,' ',st,' ',dr);
close(f);
close(g);
end;
end.