public long solution(int n, int[] works) {
long answer = 0;
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
for (int work : works) {
maxHeap.offer(work);
}
while (n > 0) {
int maxWork = maxHeap.poll();
if (maxWork == 0)
break;
maxHeap.offer(maxWork - 1);
n--;
}
for (Integer work : maxHeap) {
answer += (long) work * work;
}
return answer;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/12927