-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_2805.java
41 lines (34 loc) · 1008 Bytes
/
number_2805.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
import java.util.Arrays;
import java.util.Scanner;
public class number_2805 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int length = sc.nextInt();
int tree_arr[] = new int[n];
for(int i=0;i<n;i++){
tree_arr[i] = sc.nextInt();
}sc.close();
int min=0;
int max=Arrays.stream(tree_arr).max().getAsInt() - 1;
int mid=0;
int result=0;
while(min<=max){
long sum=0;
mid = (min+max)/2; //최소길이와 최대길이의 평균
for(int i=0;i<tree_arr.length;i++){
if(tree_arr[i]>=mid){
sum+=tree_arr[i]-mid;
}
}
if(sum<length){
max=mid-1;
}
else{
min=mid+1;
result = mid;
}
}
System.out.println(result);
}
}