Description–
解题思路–
二分答案
代码–
#include <iostream>
#include <cstdio>
#define ll long long
using namespace std;
ll n, m, l, r, a[100005];
bool hll(ll x)
{
ll s = 0, k = 1;
for (ll i = 1; i <= n; ++i)
{
if (s + a[i] <= x) s += a[i];
else s = a[i], k++;
}
return k <= m;
}
int main()
{
scanf("%lld%lld", &n, &m);
for (ll i = 1; i <= n; ++i)
scanf("%lld", &a[i]), l = max(l, a[i]), r += a[i];
while (l < r)
{
ll mid = (l + r) / 2;
if (hll(mid)) r = mid;
else l = mid + 1;
}
printf("%lld", l);
return 0;
}