Skip to content

Commit ee5b276

Browse files
authored
Merge pull request #1839 from AlgorithmWithGod/zinnnn37
[20260126] BOJ / G5 / 기타 레슨 / 김민진
2 parents 73a5bf9 + d17071e commit ee5b276

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
```java
2+
import java.io.*;
3+
import java.util.StringTokenizer;
4+
5+
public class BJ_2343_기타_레슨 {
6+
7+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
private static StringTokenizer st;
10+
11+
private static int N, M, left, right, ans;
12+
private static int[] lessons;
13+
14+
public static void main(String[] args) throws IOException {
15+
init();
16+
sol();
17+
}
18+
19+
private static void init() throws IOException {
20+
st = new StringTokenizer(br.readLine());
21+
N = Integer.parseInt(st.nextToken());
22+
M = Integer.parseInt(st.nextToken());
23+
24+
left = right = 0;
25+
ans = Integer.MAX_VALUE;
26+
lessons = new int[N];
27+
28+
st = new StringTokenizer(br.readLine());
29+
for (int i = 0; i < N; i++) {
30+
lessons[i] = Integer.parseInt(st.nextToken());
31+
left = Math.max(left, lessons[i]);
32+
right += lessons[i];
33+
}
34+
}
35+
36+
private static void sol() throws IOException {
37+
while (left <= right) {
38+
int mid = left + (right - left) / 2;
39+
40+
if (isRecordable(mid)) {
41+
ans = Math.min(ans, mid);
42+
right = mid - 1;
43+
} else {
44+
left = mid + 1;
45+
}
46+
}
47+
48+
bw.write(ans + "");
49+
bw.flush();
50+
bw.close();
51+
br.close();
52+
}
53+
54+
private static boolean isRecordable(int mid) {
55+
int cnt = 1;
56+
int sum = 0;
57+
58+
for (int lesson : lessons) {
59+
if (sum + lesson <= mid) {
60+
sum += lesson;
61+
} else {
62+
cnt++;
63+
sum = lesson;
64+
}
65+
}
66+
67+
return cnt <= M;
68+
}
69+
70+
}
71+
```

0 commit comments

Comments
 (0)