提交时间:2023-11-08 13:37:49

运行 ID: 109362

#include<bits/stdc++.h> #define int long long using namespace std; int a[114514],n,m; bool check(int x){ int sum = 0,cnt = 0; for (int i = 1;i <= n;i++){ if (a[i] > x){ return false; } else if (sum + a[i] > x){ cnt++; sum = a[i]; } else if (sum + a[i] <= x){ sum += a[i]; } } if (sum != 0){ cnt++; } return (cnt <= m); } signed main(){ cin>>n>>m; for (int i = 1;i <= n;i++){ cin>>a[i]; } int l = 0,r = 1e18; while (l <= r){ int mid = (l + r) / 2; if (check(mid)){ r = mid - 1; } else{ l = mid + 1; } } cout<<l; return 0; }