-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1085.cpp
More file actions
53 lines (49 loc) · 816 Bytes
/
cses_1085.cpp
File metadata and controls
53 lines (49 loc) · 816 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
53
#include <bits/extc++.h>
using namespace std;
int n, k;
vector<int> a;
bool judge(int64_t sep)
{
int cnt = 0;
int64_t sum = 0;
for (int x : a)
{
if (x > sep)
return false;
if (sum + x > sep)
{
cnt++;
sum = 0;
}
sum += x;
}
if (sum != 0)
cnt++;
return cnt <= k;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int64_t l = 0, r = 0, ans = 0;
cin >> n >> k;
a.resize(n);
for (auto &x : a)
{
cin >> x;
r += x;
}
while (l <= r)
{
int64_t mid = (l + r) / 2;
if (judge(mid))
{
r = mid - 1;
ans = mid;
}
else
l = mid + 1;
}
cout << ans << '\n';
return 0;
}