感觉题解都说复杂了,琢磨了很久才搞明白,所以给大家说一下我的理解: 首先判断 1:包含最右边那个数的子序列能否大于0 2:包含最左边那个数的子序列能否大于0 如果满足那说明,最大序列有可能头尾相接 然后简单判断一下 1:普通的最大值 2:所有数之和 - 普通的最小值 这两个哪个大就输出哪个就行了 import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { int n = nextInt(); int[] list = new int[n]; int sum = 0; for (int i = 0; i < n; i++) { list[i] = nextInt(); sum += list[i]; } int dp_left = Integer.MIN_VALUE/2; int dp_right = Integer.MIN_VALUE/2; int dp_min = Integer.MAX_VALUE/2; int min = Integer.MAX_VALUE/2; int max = Integer.MIN_VALUE/2; for (int i = 0; i < n; i++) { dp_left = Math.max(list[i], dp_left + list[i]); max = Math.max(dp_left, max); } for (int i = n-1; i >= 0; i--) dp_right = Math.max(list[i], dp_right + list[i]); for (int i = 0; i < n; i++) { dp_min = Math.min(list[i], dp_min + list[i]); min = Math.min(dp_min, min); } int ans = 0; if(dp_left>0 && dp_right>0) ans = Math.max(sum - min, max); else ans = max; System.out.println(ans); } static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer st = new StreamTokenizer(br); static int nextInt() throws IOException { st.nextToken(); return (int)st.nval; } static long nextLong() throws IOException { st.nextToken(); return (long)st.nval; } }