Advertisement
tien_noob

Array Division (CSES)

Feb 17th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <numeric>
  5. #include <cmath>
  6. #include <queue>
  7. using namespace std;
  8. const int N = 2e5;
  9. long long a[N+1], n, k;
  10. void read()
  11. {
  12.    cin >> n >> k;
  13.    for (int i = 1; i <= n; ++ i)
  14.    {
  15.        cin >> a[i];
  16.    }
  17. }
  18. bool check(long long x)
  19. {
  20.     int cnt = 0, cur = 1;
  21.     while (cnt < k && cur <= n)
  22.     {
  23.         long long sum = 0;
  24.         while (cur <= n && sum + a[cur] <= x)
  25.         {
  26.             sum += a[cur];
  27.             ++cur;
  28.         }
  29.         ++cnt;
  30.     }
  31.     return cur == n + 1;
  32. }
  33. void solve()
  34. {
  35.    long long low = 0, high = 1e18;
  36.    while (low <= high)
  37.    {
  38.        long long mid = (low + high)/2;
  39.        if (check(mid))
  40.        {
  41.            high = mid - 1;
  42.        }
  43.        else
  44.        {
  45.            low = mid + 1;
  46.        }
  47.    }
  48.    cout << low;
  49. }
  50. int main()
  51. {
  52.     ios_base::sync_with_stdio(false);
  53.     cin.tie(nullptr);
  54.     read();
  55.     solve();
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement