forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8.java
More file actions
42 lines (35 loc) Β· 1.3 KB
/
8.java
File metadata and controls
42 lines (35 loc) Β· 1.3 KB
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
36
37
38
39
40
41
42
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// λ‘μ κ°μ(N)μ μμ²ν λ‘μ κΈΈμ΄(M)
int n = sc.nextInt();
int m = sc.nextInt();
// κ° λ‘μ κ°λ³ λμ΄ μ 보
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// μ΄μ§ νμμ μν μμμ κ³Ό λμ μ€μ
int start = 0;
int end = (int) 1e9;
// μ΄μ§ νμ μν (λ°λ³΅μ )
int result = 0;
while (start <= end) {
long total = 0;
int mid = (start + end) / 2;
for (int i = 0; i < n; i++) {
// μλμ λμ λ‘μ μ κ³μ°
if (arr[i] > mid) total += arr[i] - mid;
}
if (total < m) { // λ‘μ μμ΄ λΆμ‘±ν κ²½μ° λ λ§μ΄ μλ₯΄κΈ°(μΌμͺ½ λΆλΆ νμ)
end = mid - 1;
}
else { // λ‘μ μμ΄ μΆ©λΆν κ²½μ° λ μλ₯΄κΈ°(μ€λ₯Έμͺ½ λΆλΆ νμ)
result = mid; // μ΅λν λ μλμ λκ° μ λ΅μ΄λ―λ‘, μ¬κΈ°μμ resultμ κΈ°λ‘
start = mid + 1;
}
}
System.out.println(result);
}
}