fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. int sol(int gg , int kk , int x){
  7. int low = 1;
  8. int high = x;
  9. int target = gg/kk;
  10. int ans = -1;
  11.  
  12. while(low<=high){
  13. int mid = (low+high)/2;
  14. int a = mid*mid - mid + 1;
  15. if(a<=target){
  16. ans = mid;
  17. low = mid+1;
  18. }
  19. else{
  20. high = mid-1;
  21. }
  22. }
  23.  
  24. return ans;
  25.  
  26. }
  27. bool check(vector<int> &arr , int x , int mid){
  28.  
  29.  
  30. int n = arr.size();
  31. int cnt=0;
  32.  
  33. for(int i=0;i<n;i++){
  34. int number = sol(mid,arr[i],x);
  35. cnt+=number;
  36.  
  37. }
  38. if(cnt>=x){
  39. return true;
  40. }
  41. return false;
  42.  
  43.  
  44. }
  45. int main() {
  46. // your code goes here
  47. int x,n;
  48. cin>>x>>n;
  49. vector<int> arr(n);
  50. int high = 0;
  51.  
  52. for(int i=0;i<n;i++){
  53. cin>>arr[i];
  54. high = max(high,arr[i]);
  55.  
  56. }
  57. high = high*(x*x - x + 1);
  58.  
  59. int low = 1;
  60. int ans = -1;
  61.  
  62. while(low<=high){
  63. int mid = (low+high)/2;
  64. if(check(arr,x,mid)==true){
  65. ans=mid;
  66. high=mid-1;
  67.  
  68. }
  69. else{
  70. low = mid+1;
  71.  
  72. }
  73. }
  74.  
  75. cout<<ans<<endl;
  76.  
  77.  
  78.  
  79.  
  80. return 0;
  81. }
Success #stdin #stdout 0s 544KB
stdin
10 4 
1 2 3 4
stdout
13