-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
63 lines (50 loc) Β· 1.56 KB
/
Main.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package TimeComplexity.P2805;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N, M;
static int[] trees;
public static void main(String[] args) throws Exception {
// System.setIn(new FileInputStream("src/TimeComplexity/P2805/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
trees = new int[N];
int max = 0;
for (int i = 0; i < N; i++) {
trees[i] = Integer.parseInt(st.nextToken());
max = Math.max(trees[i], max);
}
int start = 0;
int end = max;
int mid;
long sum = 0;
int result = 0;
while (start <= end) {
mid = (start + end) / 2;
sum = calc(mid);
if (sum == M) {
result = mid;
break;
} else if (sum > M) {
result = mid;
start = mid + 1;
} else {
end = mid - 1;
}
}
System.out.println(result);
}
static long calc(int value) {
long result = 0;
for (int i = 0; i < N; i++) {
if(trees[i] > value)
result += trees[i] - value;
}
return result;
}
}