import java.util.Comparator;
import java.util.PriorityQueue;

/**
* 输入正数数组cost表示每个项目的花费,正数数组profits表示每个项目的利润,M表示初始资金,K表示最多只能串行做k个项目
* 返回最后获得的最大钱数
*/
public class IPO {

public static int findMaximizedCapital(int k, int m, int[] profits, int[] costs) {
PriorityQueue<Program> minCostQ = new PriorityQueue<>(Comparator.comparingInt(a -> a.cost));
PriorityQueue<Program> maxProfitQ = new PriorityQueue<>((a, b) -> b.profit - a.profit);
for (int i = 0; i < profits.length; i++) {
minCostQ.add(new Program(profits[i], costs[i]));
}
for (int i = 0; i < k; i++) {
while (!minCostQ.isEmpty() && minCostQ.peek().cost <= m) {
maxProfitQ.add(minCostQ.poll());
}
if (maxProfitQ.isEmpty()) {
return m;
}
m += maxProfitQ.poll().profit;
}
return m;
}

public static class Program {

// 纯利润
public int profit;

// 花费
public int cost;

public Program(int profit, int cost) {
this.profit = profit;
this.cost = cost;
}

}

}

/* 如有意见或建议,欢迎评论区留言;如发现代码有误,欢迎批评指正 */