思路:二分答案经典题吧。。。。注意边界就OK了
//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
int n,m,a[100050],ans;
bool check(int x){
int jy=0,temp=0;
for(int i=1;i<=n;i++){
if(a[i]+jy>x){
jy=0;
temp++;
if(temp>m)return 0;
}
jy+=a[i];
if(temp>=m||jy>x)return 0;
}
return 1;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int Left=0,Right=0x3fffffff;
while(Left<=Right){
int Mid=(Left+Right)>>1;
if(check(Mid))Right=Mid-1,ans=Mid;
else Left=Mid+1;
}
printf("%d\n",ans);
}