-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path예산.java
More file actions
56 lines (43 loc) · 1.42 KB
/
예산.java
File metadata and controls
56 lines (43 loc) · 1.42 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.*;
import java.util.*;
public class Main {
static int n, m, total, answer;
static int[] states;
public static void main(String[] args) throws Exception {
preSetting();
if (m < total) binarySearch();
System.out.println(answer);
}
static void binarySearch() {
int s, e, budget, sub;
s = 0;
e = answer;
while (s <= e) {
budget = (s + e) / 2;
sub = calculation(budget);
if (m < total - sub) e = budget - 1;
else s = budget + 1;
if (total - sub <= m) answer = budget;
}
}
static int calculation(int maxBudget) {
int sum = total;
for (int i = 0; i < n; i++) {
if (maxBudget < states[i]) sum -= maxBudget;
else sum -= states[i];
}
return sum;
}
static void preSetting() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
states = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
states[i] = Integer.parseInt(st.nextToken());
answer = Math.max(answer, states[i]);
total += states[i];
}
m = Integer.parseInt(br.readLine());
}
}