fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ms(s,n) memset(s,n,sizeof(s))
  5. #define all(a) a.begin(),a.end()
  6. #define present(t, x) (t.find(x) != t.end())
  7. #define sz(a) int((a).size())
  8. #define FOR(i, a, b) for (int i = (a); i < (b); ++i)
  9. #define FORd(i, a, b) for (int i = (a) - 1; i >= (b); --i)
  10. #define pb push_back
  11. #define pf push_front
  12. #define fi first
  13. #define se second
  14. #define mp make_pair
  15.  
  16. typedef long long ll;
  17. typedef unsigned long long ull;
  18. typedef long double ld;
  19. typedef pair<int,int> pi;
  20. typedef vector<int> vi;
  21. typedef vector<pi> vii;
  22.  
  23. const int MOD = (int) 1e9+7;
  24. const int INF = (int) 1e9+1;
  25. inline ll gcd(ll a,ll b){ll r;while(b){r=a%b;a=b;b=r;}return a;}
  26. inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
  27.  
  28.  
  29. bool check(ll a[], int n, ll val, int k){
  30. int cnt = 0 ;
  31. ll sum = 0;
  32. FOR(i, 0, n){
  33. if(a[i] > val) return false;
  34. sum += a[i];
  35. if(sum > val){
  36. ++cnt;
  37. sum = a[i];
  38. }
  39. if(cnt > k) return false;
  40. }
  41. ++cnt;
  42. return cnt <= k;
  43. }
  44.  
  45.  
  46. int main(){
  47. #ifndef ONLINE_JUDGE
  48. freopen("input.txt", "r", stdin);
  49. freopen("output.txt", "w", stdout);
  50. #endif
  51. int n, k; cin >> n >> k;
  52. ll a[n];
  53. for(ll &x : a) cin >> x;
  54. ll left = *max_element(a, a + n);
  55. ll right = accumulate(a, a + n, 0ll);
  56. ll res;
  57. while(left <= right){
  58. ll mid = (left + right) / 2;
  59. if(check(a, n , mid, k)){
  60. res = mid;
  61. right = mid - 1;
  62. }
  63. else{
  64. left = mid + 1;
  65. }
  66. }
  67. cout << res << endl;
  68. return 0;
  69. }
Time limit exceeded #stdin #stdout 5s 5376KB
stdin
Standard input is empty
stdout
Standard output is empty