#include<bits/stdc++.h>
using namespace std;
bool check(int* num,int n,int m,int mid){
int tmp=0,ans=1;//ans初始值为1,因为至少有一个月
for(int i=0;i<n;i++){
if(tmp+num[i]<=mid) tmp+=num[i];
else if(num[i]<=mid){
ans ++ ;
tmp = num[i];
if(ans>m) return false;//mid太小
}
else return false ; //mid太小
}
if(ans>m) return false;
else if(ans<=m) return true ;
}
int main(){
int n,m,num[100000+10];
cin >> n >> m;
int low=0,height=0,mid;
for(int i=0;i<n;i++){
cin >> num[i];
if(num[i]>low) low = num[i];
height += num[i];
}
while(low<height){
mid = (low+height)/2;
if(check(num,n,m,mid)){
height = mid;
}else{
low = mid + 1;
}
}
printf("%d\n",low);
return 0;
}
月度开销
最新推荐文章于 2024-03-10 23:26:20 发布