-
Notifications
You must be signed in to change notification settings - Fork 6
/
HyeKyoung.java
38 lines (32 loc) · 970 Bytes
/
HyeKyoung.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class HyeKyoung {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]), m = Integer.parseInt(str[1]);
int max = Integer.MIN_VALUE, low, mid = 0, high;
long sum = 0;
int[] height = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
height[i] = Integer.parseInt(st.nextToken());
if (max < height[i]) max = height[i];
}
low = 1;
high = max;
while (low <= high) {
mid = (low + high) / 2;
sum = 0;
for (int i : height)
if (i > mid) sum += i - mid;
if (sum > m) low = mid + 1;
else if (sum < m) high = mid - 1;
else break;
}
if(sum < m) mid--;
System.out.println(mid);
}
}