Input
The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input. Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, …, an. (|ai| <= 10000).There is an empty line after each case. Output
Print exactly one line for each test case. The line should contain the integer d(A). Sample Input
1
10 1 -1 2 2 3 -3 4 -4 5 -5 Sample Output
13 Hint
In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.
Huge input,scanf is recommended.
import java.util.Scanner;public class MaximumSum {public static void main(String[] args) {int a[]=new int[50010];[50010];[50010];Scanner in=new Scanner(System.in);int t=in.nextInt();while (t!=0) {int n=in.nextInt();for (int i = 0; i <n; i++) {a[i]=in.nextInt();}left[0]=a[0];for (int i = 1; i <n; i++) {if (left[i-1]<0) {left[i]=a[i];}else {left[i]=left[i-1]+a[i];}}for (int i =1; i <n; i++) {left[i]=Math.max(left[i], left[i-1]);}right[n-1]=a[n-1];for (int i =n-2; i>0; i–) {if (right[i+1]<0) {right[i]=a[i];}else {right[i]=right[i+1]+a[i];}}for (int i = n-2; i>=0; i–) {right[i]=Math.max(right[i], right[i+1]);}int ans=-1000000;for (int i =1; i <n; i++) {ans=Math.max(ans, left[i-1]+right[i]);}System.out.println(ans);t–;}}}
,愈想得到,就愈要放手。放手是很难的,但是别无选择。