class Solution { public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) { int len = profits.length; int now = w; int cnt = k; int[][] projects = new int[len][2]; for (int i = 0; i < len; i++) { projects[i][0] = profits[i]; projects[i][1] = capital[i]; } Arrays.sort(projects, Comparator.comparingInt(o -> o[1])); PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2 - o1); for (int[] project : projects) { while (project[1] > now) { if (cnt == 0 || pq.isEmpty()) { return now; } cnt--; now += pq.poll(); } pq.offer(project[0]); } for (int i = 0; i < cnt && !pq.isEmpty(); i++) { now += pq.poll(); } return now; } }