File tree 1 file changed +48
-0
lines changed
1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ public class Main {
5
+
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
+ int N = Integer .parseInt (st .nextToken ());
10
+ int M = Integer .parseInt (st .nextToken ());
11
+ st = new StringTokenizer (br .readLine ());
12
+ int [] lectures = new int [N ];
13
+ int sum = 0 ;
14
+ for (int i = 0 ; i < N ; i ++) {
15
+ lectures [i ] = Integer .parseInt (st .nextToken ());
16
+ sum += lectures [i ];
17
+ }
18
+ int left = lectures [0 ];
19
+ int right = sum ;
20
+ int answer = sum ;
21
+ while (left <= right ) {
22
+ int mid = (left + right ) / 2 ;
23
+ int time = 0 ;
24
+ int count = 0 ;
25
+ int maxTime = left ;
26
+ for (int i = 0 ; i < N ; i ++) {
27
+ if (time + lectures [i ] > mid ) {
28
+ maxTime = Math .max (maxTime , time );
29
+ time = lectures [i ];
30
+ count ++;
31
+ } else {
32
+ time += lectures [i ];
33
+ }
34
+ }
35
+ if (time > 0 ) {
36
+ count ++;
37
+ maxTime = Math .max (maxTime , time );
38
+ }
39
+ if (count <= M ) {
40
+ answer = Math .min (answer , maxTime );
41
+ right = mid - 1 ;
42
+ } else {
43
+ left = mid + 1 ;
44
+ }
45
+ }
46
+ System .out .println (answer );
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments