-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHW_12927.java
More file actions
32 lines (27 loc) Β· 956 Bytes
/
HW_12927.java
File metadata and controls
32 lines (27 loc) Β· 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
// n : ν΄κ·Όμκ°κΉμ§ λ¨μ μκ°
// works : μμ
λ
class HW_12927 {
public long solution(int n, int[] works) {
long answer = 0; // μΌκ·Ό νΌλ‘λ = μμν μμ μμ (λ¨μ μΌμ μμ
λ)^2
PriorityQueue<Integer> Queue = new PriorityQueue<>(Comparator.reverseOrder()); // λμ μ«μ μ°μ μμ ν μ μ
for(int i=0; i<works.length; i++){
Queue.offer(works[i]); // offer() : μ°μ μμ νμ μμ μΆκ°, μ€ν¨μ false λ°ν
}
while(n>0){
int work = Queue.poll();
if(work==0) {
break;
}
work -= 1;
Queue.offer(work);
n -=1;
}
int size = Queue.size(); // Queue size μ€μ΄λλ κ² λ°©μ§
for(int i=0; i<size; i++){
int work = Queue.poll();
answer += work * work;
}
return answer;
}
}