Advertisement
newb_ie

bs5

Sep 2nd, 2021
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN = 2 * 1e5 + 10;
  5. long long a[maxN];
  6. int n;
  7.  
  8. bool yes (long long block_limit,long long k) {
  9. long long sum = 0;
  10. long long count = 0;
  11. for (int i = 1; i <= n; ++i) {
  12. sum += a[i];
  13. if (sum > block_limit) {
  14. sum = a[i];
  15. ++count;
  16. }
  17. }
  18. if (sum > 0) ++count;
  19. if (count <= k) return true;
  20. else return false;
  21. }
  22.  
  23.  
  24. int main () {
  25. ios::sync_with_stdio(false);
  26. cin.tie(nullptr);
  27. cout.tie(nullptr);
  28. long long k;
  29. cin >> n >> k;
  30. for (int i = 1; i <= n; ++i) cin >> a[i];
  31. long long l = 0,r = 1e15;
  32. for (int i = 1; i <= n; ++i) l = max(l,a[i]);
  33. long long res = 0;
  34. while (l <= r) {
  35. long long m = l + (r - l) / 2;
  36. if (yes(m,k)) {
  37. res = m;
  38. r = m - 1;
  39. } else {
  40. l = m + 1;
  41. }
  42. }
  43. cout << res << '\n';
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement