最大连续数列的和
Time Limit:1000MS Memory Limit:65536K
Total Submit:142 Accepted:83
Description
求最大连续子序列的和
Input
第一行输入n(n<=500),第二行为n个以空格分开的整数(-1000到1000之间);
Output
该序列中最大的连续子序列的和
Sample Input
6
1 2 -5 6 7 8
Sample Output
21
-
var a,f:array[0..1000] of longint; n,m,j,i,max:longint; begin readln(n); for i:=1 to n do read(a[i]); for i:=1 to n do begin if (a[i]+f[i-1]>0) and (f[i-1]<>0) then f[i]:=a[i]+f[i-1]; if (a[i]+f[i-1]<0) and (f[i-1]<>0) then f[i]:=0; if f[i-1]=0 then if a[i]>0 then f[i]:=a[i] else f[i]:=0; end; for i:=1 to n do if f[i]>max then max:=f[i]; write(max); end.