import java.util.Collections;
import java.util.PriorityQueue;
// 야근 지수 - 연습문제 (heap)
public class NightShift {
public long solution(int n, int[] works) {
long answer = 0;
PriorityQueue<Integer> heap = new PriorityQueue<>(Collections.reverseOrder());
for (int ele : works) {
heap.offer(ele);
}
for (int i = 0; i < n; i++) { // 최대 힙을 이용하여 최댓값을 1씩 감소하는 것을 n번 반복
heap.offer(heap.poll() - 1);
}
if (heap.peek() <= 0) { // 예외 처리
return 0;
}
while (!heap.isEmpty()) {
answer += Math.pow(heap.poll(), 2);
}
return answer;
}
}