Java AC
import java.util.Scanner; public class Main { /* * 1376 */ public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int array[] = new int[n]; int result = -1; for (int i = 0; i < n; i++) { array[i] = scanner.nextInt(); if (array[i] == 0) { result = 0; } } if (result == 0) { System.out.println(result); continue; } result = array[0]; for (int i = 0; i < n; i++) { int sum = 0; for (int j = i; j < n; j++) { sum += array[j]; int tempSum1 = Math.abs(sum); int tempSum2 = Math.abs(result); if (tempSum1 < tempSum2 || (tempSum1 == tempSum2 && sum > result)) { result = sum; if (result == 0) { break; } } } if (result == 0) { break; } } System.out.println(result); } } } /************************************************************** Problem: 1376 User: wangzhenqing Language: Java Result: Accepted Time:1730 ms Memory:105284 kb ****************************************************************/C++ AC
#include <stdio.h> const int maxn = 100005; int array[maxn]; int abs(int x){ return x < 0 ? x * -1 : x; } int main(){ int n,i,j; while(scanf("%d",&n) != EOF){ int result = -1; for(i = 0; i < n; i++){ scanf("%d",array+i); if(array[i] == 0){ result = 0; } } if(result == 0){ printf("%d\n",result); continue; } result = array[0]; for (i = 0; i < n; i++) { int sum = 0; for (j = i; j < n; j++) { sum += array[j]; int tempSum1 = abs(sum); int tempSum2 = abs(result); if (tempSum1 < tempSum2 || (tempSum1 == tempSum2 && sum > result)) { result = sum; if (result == 0) { break; } } } if (result == 0) { break; } } printf("%d\n",result); } return 0; } /************************************************************** Problem: 1376 User: wangzhenqing Language: C++ Result: Accepted Time:140 ms Memory:1412 kb ****************************************************************/