-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Division.cpp
More file actions
61 lines (57 loc) · 1.44 KB
/
Array_Division.cpp
File metadata and controls
61 lines (57 loc) · 1.44 KB
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
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
#define int unsigned long long
#define forn(i, k, e) for (int i = k; i < e; i++)
#define dbg(x) cout << #x << " = " << x << endl
/*
sum of elements in the sub array should be atmost mid
smax = max of sum of a single subarray
ans = min(ans, smax) if count of subarrays <= k
*/
int32_t main() {
int n, k;
cin >> n >> k;
vector<int> nums(n);
int hi = 0, lo = 0, ans = 1e11;
forn(i, 0, n) {
cin >> nums[i];
hi += nums[i];
lo = max(lo, nums[i]);
}
if (k == 1) {
cout << hi << endl;
return 0;
}
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int cnt = 0;
int sum = 0;
int smax = 0;
forn(i, 0, n) {
int cur = nums[i];
if (cur >= mid) {
cnt++;
if (sum > 0)
cnt++;
smax = max({smax, cur, sum});
sum = 0;
} else
sum += cur;
if (sum >= mid) {
cnt += 1;
smax = max({smax, sum == mid ? mid : sum - cur});
sum = sum == mid ? 0 : cur;
}
smax = max(smax, sum);
}
cnt += sum > 0;
smax = max(smax, sum);
if (cnt <= k) {
ans = min(ans, smax);
hi = mid - 1;
} else
lo = mid + 1;
}
cout << ans << endl;
return 0;
}