fork 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. bool isPossible(int n, int k, int mid){
  26. int sum = 0;
  27. int ct = 1;
  28. for(int i=0; i<n; i++){
  29. if(a[i] > mid) return false;
  30. if(sum + a[i] <= mid){
  31. sum += a[i];
  32. }
  33. else{
  34. sum = a[i];
  35. ct++;
  36. }
  37. }
  38. return ct <= k;
  39. }
  40.  
  41. int consistency(int n, int k){
  42.  
  43. int s = 0, e = INF;
  44. while(s<e){
  45. int mid = s + (e-s)/2;
  46. if(isPossible(n, k, mid)){
  47. e = mid;
  48. }
  49. else s = mid+1;
  50. }
  51.  
  52. return e;
  53.  
  54. }
  55.  
  56. void solve() {
  57.  
  58. int n, k;
  59. cin>>n >> k;
  60.  
  61. a.resize(n);
  62.  
  63. for(int i=0; i<n; i++) cin >> a[i];
  64. cout << consistency(n, k) << endl;
  65.  
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73. int32_t main() {
  74. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  75.  
  76. int t = 1;
  77. // cin >> t;
  78. while (t--) {
  79. solve();
  80. }
  81.  
  82. return 0;
  83. }
Success #stdin #stdout 0s 5324KB
stdin
5 3
2 4 7 3 5
stdout
8