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