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