版权声明:转载请注明出处 https://blog.csdn.net/TY_GYY/article/details/83385439
import java.util.*;
public class Main{
Scanner scan=new Scanner(System.in);
int n,m ,L=0,R=0;
int[] s=new int [100010];
public Main() {
super();
input();
js();
}
public void input() {
n=scan.nextInt();
m=scan.nextInt();
for (int i = 0; i < n; i++) {
s[i]=scan.nextInt();
L=Math.max(L, s[i]);
R+=s[i]; //出错地方 不要想当然
}
}
public boolean judge(int mid) {
int sum=0,j=0;
for (int i = 0; i < n;i++) {
if(sum+s[i]>mid) {
j++;
sum=s[i];
}
else {
sum+=s[i];
}
}
if(++j<=m) return true;
else return false;
}
public void js() {
while(L<R) {
int mid=L+(R-L)/2;
if(judge(mid))
R=mid;
else
L=mid+1;
}
System.out.println(R);
}
public static void main(String[] args) {
new Main();
}
}