-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_2343.java
More file actions
49 lines (39 loc) · 1.24 KB
/
BOJ_2343.java
File metadata and controls
49 lines (39 loc) · 1.24 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
import java.io.*;
import java.util.*;
public class BOJ_2343 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[] arr = new int[n];
int start = 0;
int end = 0;
st = new StringTokenizer(br.readLine());
for(int i = 0; i < n; i++) {
int num = Integer.parseInt(st.nextToken());
arr[i] = num;
if(start < num) start = num;
end += num;
}
// System.out.println("start = " + start + " end = " + end);
while(start <= end) {
int mid = (start + end) / 2;
int sum = 0;
int cnt = 1;
for(int i = 0; i < n; i++) {
if(sum + arr[i] > mid) {
cnt++;
sum = 0;
}
sum += arr[i];
}
if(cnt > m) {
start = mid + 1;
} else {
end = mid - 1;
}
}
System.out.println(start);
}
}