题解 | #连续子数组最大和(ACM版本)#
连续子数组最大和(ACM版本)
http://www.nowcoder.com/practice/1718131e719746e9a56fb29c40cc8f95
import java.util.*;
public class Main{
public static long gexMaxSubArraySum(int n,long[] array){
long dp = array[0];
long maxSum = array[0];
long sum = array[0];
for(int i = 1;i<n;i++){
if(dp >= 0){
sum+=array[i];
dp+=array[i];
}
else{
dp = array[i];
sum = array[i];
}
maxSum = maxSum>=sum?maxSum:sum;
}
return maxSum;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[] array = new long[n];
for(int i = 0;i < n;i++){
array[i] = in.nextLong();
}
long outNu = gexMaxSubArraySum(n,array);
System.out.println(outNu);
}
}
public class Main{
public static long gexMaxSubArraySum(int n,long[] array){
long dp = array[0];
long maxSum = array[0];
long sum = array[0];
for(int i = 1;i<n;i++){
if(dp >= 0){
sum+=array[i];
dp+=array[i];
}
else{
dp = array[i];
sum = array[i];
}
maxSum = maxSum>=sum?maxSum:sum;
}
return maxSum;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[] array = new long[n];
for(int i = 0;i < n;i++){
array[i] = in.nextLong();
}
long outNu = gexMaxSubArraySum(n,array);
System.out.println(outNu);
}
}