fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6. inline int power(int a, int b) {
  7. int x = 1;
  8. while (b) {
  9. if (b & 1) x *= a;
  10. a *= a;
  11. b >>= 1;
  12. }
  13. return x;
  14. }
  15.  
  16.  
  17. const int M = 1000000007;
  18. const int N = 3e5+9;
  19. const int INF = 2e9+1;
  20. const int LINF = 2000000000000000001;
  21.  
  22. //_ ***************************** START Below *******************************
  23.  
  24. vector<int> a;
  25.  
  26.  
  27.  
  28. bool isPossible(int n, int k, int mid){
  29. int ct = 1;
  30. int sum = 0;
  31. for(int i=0; i<n; i++){
  32. if(mid < a[i]) return false;
  33. if(sum + a[i] > mid){
  34. sum = a[i];
  35. ct++;
  36. }
  37. else{
  38. sum += a[i];
  39. }
  40. }
  41. if(ct <= k) return true;
  42. return false;
  43. }
  44.  
  45. int consistency(int n, int k){
  46.  
  47. int s = 0, e = LINF;
  48. int ans = LINF;
  49. while(s<=e){
  50. int mid = s+(e-s)/2;
  51. if(isPossible(n, k, mid)){
  52. ans = min(ans, mid);
  53. e = mid-1;
  54. }
  55. else s = mid+1;
  56. }
  57.  
  58. return ans;
  59.  
  60. }
  61.  
  62. void solve() {
  63.  
  64. int n, k;
  65. cin>>n >> k;
  66.  
  67. a.resize(n);
  68.  
  69. for(int i=0; i<n; i++) cin >> a[i];
  70. cout << consistency(n, k) << endl;
  71.  
  72.  
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79. int32_t main() {
  80. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  81.  
  82. int t = 1;
  83. // cin >> t;
  84. while (t--) {
  85. solve();
  86. }
  87.  
  88. return 0;
  89. }
Success #stdin #stdout 0.01s 5296KB
stdin
5 3
2 4 7 3 5
stdout
8