[ABC319D] Minimum Width 二分答案
作者:
多米尼克領主的致意
,
2024-05-21 18:18:07
,
所有人可见
,
阅读 5
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n, m;
ll a[N];
bool check(ll x){
ll cnt = 1;
ll len = a[1];
if(a[1] > x)return 0;
for(int i = 2;i <= n;i++){
if(a[i] > x)return 0;
if(len + a[i] + 1 > x){
cnt++;
len = a[i];
}
else{
len += a[i] + 1;
}
}
return cnt <= m;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
ll sum = 0;
for(int i = 1;i <= n;i++){
cin >> a[i];
}
ll l = 1, r = 1e18;
// cout << sum << endl;
while(l < r){
ll mid = (l + r) >> 1;
if(check(mid))r = mid;
else l = mid + 1;
}
cout << r;
return 0;
}