728x90
Line:26 left값 초기화를 budget[0]으로 해줬음
5
10 10 10 10 10
49
입력하면
9가 나와야 하는데 0이 출력됨
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
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
public class Main {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
int N = stoi(br.readLine());
String[] str = br.readLine().split(" ");
int M = stoi(br.readLine());
int[] budget = new int[N];
for(int i=0; i<N; i++)
budget[i] = stoi(str[i]);
Arrays.sort(budget);
long left = 1;
long right = budget[N-1];
long res = 0;
while(left<=right) {
long mid = (left+right)/2;
long tempSum = 0;
for (int b : budget)
tempSum += Math.min(b,mid);
// System.out.println(left+" "+mid+" "+right+" : "+tempSum);
if(tempSum>M)
right = mid-1;
else {
left = mid+1;
res = mid;
}
}
System.out.println(res);
// bw.write("");
// bw.flush();
// bw.close();
}
private static int stoi(String input) {
return Integer.parseInt(input);
}
}
|
cs |
'알고리즘 풀이 > 백준' 카테고리의 다른 글
[백준][Java] 2745번 진법 변환 (문자열) (0) | 2021.10.21 |
---|---|
★ [백준][Java] 2470번 두 용액 (이진탐색) (0) | 2021.10.18 |
★ [백준][Java] 2110번 공유기 설치 (이진탐색) #2 (0) | 2021.10.16 |
[백준][Java] 10815번 숫자 카드 (이진탐색) (0) | 2021.10.15 |
★★★ [백준][Java] 1654번 랜선 자르기 (이진탐색) #2 (0) | 2021.10.15 |