第二题,每次变负数了就淘汰最小的数就行了 private static void solve(int n, int[] arr) { PriorityQueue<Integer> q = new PriorityQueue<>(); int sum = 0; int cnt = 0; for (int i = 0; i < n; i++) { sum += arr[i]; q.add(arr[i]); cnt += 1; if (sum < 0 ) { int tmp = q.poll(); sum -= tmp; cnt -= 1; } } System.out.println(cnt); }