如下
#include<bits/stdc++.h>
using namespace std;
int n,p,l=1,r,mid;
int a[100010];
bool check(int x)
{
int sum=0,now=0;
for(int i=1;i<=n;i++)
{
if(sum+a[i]<=x) sum=sum+a[i];
else sum=a[i],now++;
}
if(now>=p)return true;
else return false;
}
int main()
{
cin>>n>>p;
for(int i=1;i<=n;i++)
{
cin>>a[i];
r=r+a[i];
}
while(l<=r)
{
mid=l+r>>1;
if(check(mid)) l=mid+1;
else r=mid-1;
}
cout<<l;
}