Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- long long int k, n;
- bool check(long long int x, const vector<int>& a) {
- int i = 0;
- long long int counter = 0;
- long long contain = 0;
- while(i < n) {
- contain = 0;
- while(contain <= x && i < n) {
- if(a[i] <= x - contain) {
- contain += a[i];
- i++;
- } else break;
- }
- if(contain == 0 && a[i] > x) return false;
- counter++;
- }
- if(counter <= k) return true;
- return false;
- }
- int main() {
- cin >> k >> n;
- vector<int> a;
- long long s = 0;
- for (int i = 0; i < n; i++) {
- int t;
- cin >> t;
- s += t;
- a.push_back(t);
- }
- sort(a.begin(), a.end());
- long long int l, r;
- l = 1;
- r = s;
- while(r - l > 1) {
- long long int m = (l + r) / 2;
- if(check(m, a)) r = m;
- else l = m;
- }
- cout << r;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement