-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek4_Day2.java
More file actions
39 lines (38 loc) · 1.03 KB
/
Week4_Day2.java
File metadata and controls
39 lines (38 loc) · 1.03 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
package Algorithm4;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collection;
import java.util.StringTokenizer;
/// 딱 정확히 설정시킬수 있는 높이의 최댓값을 구하시오.
public class Week4_Day2{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] info = br.readLine().split(" ");
int N = Integer.parseInt(info[0]);
int M = Integer.parseInt(info[1]);
int[] h = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0; i<N; i++) {
h[i] = Integer.parseInt(st.nextToken());
}
int left = 0;
int right = Arrays.stream(h).max().getAsInt();
while(left <= right) {
long total = 0;
int mid = (left + right)/2;
for(int j=0; j<N; j++) {
if(mid < h[j]) {
total+= (h[j]-mid);
}
}
if (total < M) {
right = mid-1;
}else {
left = mid +1;
}
}
System.out.println(right);
}
}