解法一:贪心
如果加上当前数小于0,那么不如不加,从头开始;否则就有继续增长的可能,继续加并更新最大值。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
final int n = Integer.parseInt(reader.readLine());
String[] strs = reader.readLine().split(" ");
int max = 0;
int sum = 0;
for (int i = 0, num; i < n; ++i) {
num = Integer.parseInt(strs[i]);
if (sum + num < 0) {
sum = 0;
} else {
sum += num;
max = Math.max(max, sum);
}
}
System.out.println(max);
}
}