/** * 5 -1 -1 -1 -1 -1 * 7 0 6 -1 1 -6 7 -5 * 5 6 -1 5 4 -7 * g and the largest subsequence, Output Max and start and stop subscript (brute force solution, timeout time limit exceeded) * @param args the command line arguments */ public static void main (String[] args) { int n; scanner cin = new scanner ( system.in); n = cin.nextint (); for (int k =1; k <=n; k++) { int[] a = new int[1000000]; int num = cin.nextint (); a[0] = num; int length = 1; while (length <= a[0]) { num = cin.nextint (); a[length] = num; length ++; } length = a[0]; int indexbegin = 1; int indexNext = 1; int sum = a[1]; for (int j = 1; j <= length; j++ ) { int temp = 0; int tempSum = 0; for (int i = j; i <= length; i++) { temp = tempSum; tempSum = tempSum + a[i]; if (( tempsum >= temp) && (tempsum > sum)) { sum = tempSum; indexBegin = j; indexnext = i; } } } system.out.println ("case " + k + ': ' ) ; system.out.println ( sum + " " +indexBegin + " " +indexnext); } }
1003 Brute Force Solution