知识点:二分
这个就是标准的最小化最大值,用二分就可以了
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, a[N];
bool check(int x) {
int cnt = 1;
int cur = a[0];
if (a[0] > x) return false;
for (int i = 1; i < n; i++) {
if (a[i] > x) return false;
if (cur + a[i] > x) {
cnt++;
cur = a[i];
} else {
cur += a[i];
}
}
return cnt <= m;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
int l = 0, r = 1e9;
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
cout << l;
return 0;
}