2
private long mod = 1000000007L;4
public int maxProfit(int[] inventory, int orders) {5
// we use pq to find the most balls6
PriorityQueue<Long> pq = new PriorityQueue<>((x, y) -> Long.compare(y, x));9
// we use map to count the balls10
Map<Long, Long> map = new HashMap<>();13
for (int j : inventory) {15
if (map.containsKey(i)) {16
map.put(i, map.get(i) + 1);25
long ball = pq.poll(), nextBall = pq.peek();26
long times = map.get(ball);27
long diff = Math.min(ball - nextBall, orders / times);29
res = (res + orders * ball) % mod;32
long sum = (ball * 2 + 1 - diff) * diff / 2 * times;33
res = (res + sum) % mod;34
orders -= diff * times;35
if (!map.containsKey(ball - diff)) {36
map.put(ball - diff, map.get(ball));37
pq.offer(ball - diff);39
map.put(ball - diff, map.get(ball - diff) + map.get(ball));