提交时间:2023-12-12 13:56:50
运行 ID: 115611
#include<bits/stdc++.h> using namespace std; const int N=1e5+10,INF=0x3f3f3f3f; long long maxx; int minn=INF,maxn=-INF,a[N],n,m; bool check(long long x) { int sum=0,f=1; for(int i=1;i<=n;i++){ sum+=a[i]; if(sum>x){ sum=a[i]; f++; } } return f<=m; } int main() { cin>>n>>m; for(int i=1;i<=n;i++){ cin>>a[i]; maxx+=a[i]; maxn=max(maxn,a[i]); minn=min(minn,a[i]); } if(n==m){ cout<<maxn; return 0; } long long l=minn,r=maxx+1; while(l<r){ long long mid=(l+r)>>1; if(check(mid)) r=mid; else l=mid+1; } cout<<r; return 0; }