Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int maxN = 2 * 1e5 + 10;
- long long a[maxN];
- int n;
- bool yes (long long block_limit,long long k) {
- long long sum = 0;
- long long count = 0;
- for (int i = 1; i <= n; ++i) {
- sum += a[i];
- if (sum > block_limit) {
- sum = a[i];
- ++count;
- }
- }
- if (sum > 0) ++count;
- if (count <= k) return true;
- else return false;
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- long long k;
- cin >> n >> k;
- for (int i = 1; i <= n; ++i) cin >> a[i];
- long long l = 0,r = 1e15;
- for (int i = 1; i <= n; ++i) l = max(l,a[i]);
- long long res = 0;
- while (l <= r) {
- long long m = l + (r - l) / 2;
- if (yes(m,k)) {
- res = m;
- r = m - 1;
- } else {
- l = m + 1;
- }
- }
- cout << res << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement