Pagini recente » Cod sursa (job #2663022) | Cod sursa (job #3256287) | Cod sursa (job #1740382) | Cod sursa (job #2628604) | Cod sursa (job #1635021)
var tabel:array[1..100] of integer;
start,stop,i,n,bestsum:integer;
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:=-maxint;
MaxPre:=-maxint;
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.int'); reset(input);
readln(n);
for i:=1 to n do read(tabel[i]);
maxsubsequence(1,n);
write(bestsum,' ',start,' ',stop);
assign(output,'ssm.out'); rewrite(output);
close(input);
close(output);
End.