Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- #include <cmath>
- #include <queue>
- using namespace std;
- const int N = 2e5;
- long long a[N+1], n, k;
- void read()
- {
- cin >> n >> k;
- for (int i = 1; i <= n; ++ i)
- {
- cin >> a[i];
- }
- }
- bool check(long long x)
- {
- int cnt = 0, cur = 1;
- while (cnt < k && cur <= n)
- {
- long long sum = 0;
- while (cur <= n && sum + a[cur] <= x)
- {
- sum += a[cur];
- ++cur;
- }
- ++cnt;
- }
- return cur == n + 1;
- }
- void solve()
- {
- long long low = 0, high = 1e18;
- while (low <= high)
- {
- long long mid = (low + high)/2;
- if (check(mid))
- {
- high = mid - 1;
- }
- else
- {
- low = mid + 1;
- }
- }
- cout << low;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- read();
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement