问题描述
给定n个数A1,A2,A3……An,求两个数l,r满足l<=r并最大化Al+A(l+1)+……Ar,输出这个最大值
输入格式
第一行一个数n,接下来一行有n个用空格隔开的数,第i个数表示Ai
输出格式
输出仅一行,即最大区间和
样例输入
4
-1 -2 -3 4
样例输出
4
数据规模和约定
每个数绝对值不超过2^30;
n<=1000000
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextInt();
long pre, now, res = Integer.MIN_VALUE;
pre = sc.nextInt();
for (int i = 1; i < n; i++) {
long a=sc.nextLong();
now = Math.max(a, pre + a);
res = Math.max(res, now);
pre = now;
}
System.out.println(res);
}