program slow(input, output); label 99; const maxNum = 20000; { don't bother if n >> 1000 } var a : array [0..maxNum] of real; n, i, j, k : integer; bestSum, sum : real; bestN, bestL, bestR : integer; { This is an appallingly slow algorithm. It is totally impractical for large data sets. It is only usable for values of n about 1000 or less, provided that you are patient. It is only given to test against its results, on small data sets. There are zero (0) marks for submitting an algorithm like this (even in C). -- This code ``slow'' is under gnu ( http://www.gnu.org/ ) ``copyleft'' GPL licence, 2/2003 L.Allison, CSSE, Monash University, Australia 3800 http://www.csse.monash.edu.au/~lloyd/ } begin n := 0; {read data} while not eof do begin while not eoln do begin read(a[n]); n := n+1; if n >= maxNum then begin writeln('error : n >= ', maxNum:1); goto 99 end end; readln; end; bestN := 0; bestSum := 0.0; bestL := 0; bestR := -1; {initialise} for i := 0 to n-1 do for j := i to n-1 do {"algorithm"} begin sum := 0.0; for k := i to j do sum := sum + a[k]; if (sum >= 0 {i.e. acceptable}) and (j-i+1 > bestN) then begin bestL := i; bestR := j; bestN := j-i+1; bestSum := sum end end; writeln('n = ', n); {results} write('part = ', bestL:1, '..', bestR:1, ' (', bestN :1, '), sum = ', bestSum:5:1, ', '); if bestN <= 6 then {short} for i := bestL to bestR do write(' ', a[i]:5:1) else {> 6, long} begin for i := bestL to bestL+2 do write(' ', a[i]:5:1); write(' ... '); for i := bestR-2 to bestR do write(' ', a[i]:5:1) end; writeln; 99: end.