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
33
34
35
|
import java.util.*;
class Solution {
public long solution(int n, int[] works) {
long answer = 0;
Arrays.sort(works);
int pos = works.length - 1;
if(works.length == 1)
return works[0] - n < 0 ? 0 : works[0] - n;
while(n > 0) {
if(pos == works.length - 1) {
if(works[pos] >= works[pos - 1]) {
works[pos]--;
n--;
} else
pos--;
} else {
if(works[pos] > works[works.length - 1]) {
works[pos]--;
n--;
} else
pos--;
}
if(pos == 0 && works[pos] == 0)
break;
if(pos < 0)
pos = works.length - 1;
}
for(int i=0;i<works.length;i++)
answer += Math.pow(works[i], 2);
return answer;
}
}
|
cs |
가장 마지막 값과 모든 원소의 값을 맞춰주어여 한다.