题目链接
二分答案转化为判定
#include<cstdio>
const int N=1e5+10;
int n,m;
int a[N];
bool judge(int mid)
{
int cnt=1,sum=0;//写成cnt=0
for(int i=1;i<=n;i++)
{
if(a[i]>mid)return false;
if(sum+a[i]<=mid)sum+=a[i];
else cnt++,sum=a[i];
}
return cnt<=m;
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int l=0,r=1e9;
while(l<r)
{
int mid=(l+r)/2;
if(judge(mid))r=mid;
else l=mid+1;
}
printf("%d\n",l);
return 0;
}