Pagini recente » Cod sursa (job #3133352) | Cod sursa (job #2132883) | Cod sursa (job #442655) | Cod sursa (job #36217) | Cod sursa (job #1635162)
const
maxn=6000000;
var tabel:array[1..maxn] of longint;
start,stop,i,n,bestsum:longint;
function max(a,b:longint):longint;
begin
if a>=b then max:=a
else max:=b;
end;
function maxsubsequence(low,hight:longint):longint;
var mid:longint;
suf,i,pre,maxsuf,left,right,maxpre,bestlow,besthight:longint;
begin
if low=hight then
begin
if bestsum<tabel[hight] then
begin
bestsum:=tabel[hight];
start:=hight;
stop:=hight;
end;
exit
end;
mid:=(low+hight)div 2;
bestLow:=maxsubsequence(low,mid);
bestHight:=maxsubsequence(mid+1,hight);
suf:=0; left:=0;
pre:=0; right:=0;
MaxSuf:=-maxn;
MaxPre:=-maxn;
for i:=mid downto low do
begin
suf:=suf+tabel[i];
if maxsuf<=suf then begin maxsuf:=suf; left:=i; end;
end;
for i:=mid+1 to hight do
begin
pre:=pre+tabel[i];
if maxpre<pre then begin maxpre:=pre; right:=i; end;
end;
//maxsubsequence:=max(bestLow,max(besthight,maxPre+maxSuf));
if maxpre+maxsuf>bestsum then
begin
bestsum:=maxpre+maxsuf;
start:=left;
stop:=right;
end;
end;
Begin
assign(input,'ssm.in'); reset(input);
readln(input,n);
for i:=1 to n do read(input,tabel[i]);
maxsubsequence(1,n);
assign(output,'ssm.out'); rewrite(output);
write(output,bestsum,' ',start,' ',stop);
close(input);
close(output);
End.