-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path11413.cpp
More file actions
35 lines (30 loc) · 711 Bytes
/
Copy path11413.cpp
File metadata and controls
35 lines (30 loc) · 711 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
#include <iostream>
#include <algorithm>
using namespace std;
int n, m, vessels[1001];
bool canFill(int cap) {
int container = 0, fill = 0;
for (int i = 0; i < n; i++) {
if (vessels[i] > cap) return false;
if (fill + vessels[i] <= cap) {
fill += vessels[i];
} else {
container++; fill = vessels[i];
}
}
if (fill) container++;
return container <= m;
}
int main() {
while (cin >> n >> m) {
for (int i = 0; i < n; i++) cin >> vessels[i];
int low = 0, high = 1100000000, mid, ans;
while (low < high) {
mid = (low + high) / 2;
if (canFill(mid)) { ans = mid; high = mid; }
else low = mid + 1;
}
cout << ans << endl;
}
return 0;
}