프로그래머스-야근지수

이호영·2022년 4월 9일
0

프로그래머스-Level.3

목록 보기
12/14
import java.util.*;

class Solution {
    public long solution(int n, int[] works) {
        long answer = 0;
        long max=0;
        
        PriorityQueue<Integer> q = new PriorityQueue<>(Collections.reverseOrder());
        
        for(int i=0; i<works.length; i++){
            max+=works[i];
            q.add(works[i]);
        }
        
        if(n>=max)
            return 0;
        
        while(true){
            if(n==0)
                break;
            int a=q.poll()-1;
            n--;
            q.add(a);
        }
        while(!q.isEmpty()){
            int a=q.poll();
            answer+=(a*a);
        }
        return answer;
    }
}

0개의 댓글