-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_division.cpp
More file actions
52 lines (43 loc) · 837 Bytes
/
Copy patharray_division.cpp
File metadata and controls
52 lines (43 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
#define endl '\n'
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
bool isPossible(vector<ll> &a, int &k, ll &mid) {
ll sum = 0;
int count = 0;
for (ll &x : a) {
if (x > mid)
return false;
if (sum + x > mid) {
++count;
sum = 0;
}
sum += x;
}
if (sum <= mid)
++count;
return count <= k;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<ll> a(n);
for (ll &x : a) {
cin >> x;
}
ll low = *max_element(a.begin(), a.end());
ll high = accumulate(a.begin(), a.end(), 0LL);
while (low < high) {
ll mid = low + (high - low) / 2;
if (isPossible(a, k, mid)) {
high = mid;
} else {
low = mid + 1;
}
}
cout << low << endl;
return 0;
}