AcWing 4178. 数列分段 II
原题链接
简单
作者:
魔仙哥
,
2024-12-20 14:54:31
,
所有人可见
,
阅读 1
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
#define LL long long
#define F(i,a,b) for(int i=a;i<=b;i++)
int a[N],ans,n,m;
bool check(int mid)
{
int sum = 0,cnt = 1;
for(int i = 1;i<=n;i++)
{
if(a[i] > mid) return 0;
if(sum + a[i] <= mid)
sum += a[i];
else
{
cnt++;
sum = a[i];
}
}
return cnt <= m;
}
int main()
{
cin>>n>>m;
for(int i = 1;i<=n;i++) cin>>a[i];
int l = 0,r = 1e9;
while(l <= r)
{
int mid = l+r>>1;
if(check(mid))
{
ans = mid;
r = mid-1;
}
else l = mid+1;
}
cout<<ans<<'\n';
return 0;
}