File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed
Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change 1+ ```java
2+ import java.io.*;
3+ import java.util.*;
4+
5+ public class Main {
6+ public static void main(String[] args) throws IOException {
7+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+ StringTokenizer st = new StringTokenizer(br.readLine());
9+
10+ int N = Integer.parseInt(st.nextToken());
11+ int M = Integer.parseInt(st.nextToken());
12+
13+ int[] lectures = new int[N];
14+ st = new StringTokenizer(br.readLine());
15+
16+ int left = 0;
17+ int right = 0;
18+
19+ for (int i = 0; i < N; i++) {
20+ lectures[i] = Integer.parseInt(st.nextToken());
21+ left = Math.max(left, lectures[i]);
22+ right += lectures[i];
23+ }
24+
25+ int answer = right;
26+
27+ while (left <= right) {
28+ int mid = (left + right) / 2;
29+
30+ if (canDivide(lectures, M, mid)) {
31+ answer = mid;
32+ right = mid - 1;
33+ } else {
34+ left = mid + 1;
35+ }
36+ }
37+
38+ System.out.println(answer);
39+ }
40+
41+ private static boolean canDivide(int[] lectures, int M, int size) {
42+ int count = 1;
43+ int currentSum = 0;
44+
45+ for (int lecture : lectures) {
46+ if (currentSum + lecture > size) {
47+ count++;
48+ currentSum = lecture;
49+
50+ if (count > M) {
51+ return false;
52+ }
53+ } else {
54+ currentSum += lecture;
55+ }
56+ }
57+
58+ return true;
59+ }
60+ }
61+ ```
You can’t perform that action at this time.
0 commit comments