fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. bool check(vector<int> &arr,int mid ,int k){
  8. int tot = mid*k;
  9. int sum=0;
  10. int n = arr.size();
  11.  
  12. for(int i=0;i<n;i++){
  13. sum = sum + min(mid,arr[i]);
  14. }
  15. return sum>=tot;
  16. }
  17.  
  18. int main() {
  19. // your code goes here
  20. int n,k;
  21. cin>>n>>k;
  22. vector<int> arr(n,0);
  23. int sum=0;
  24. for(int i=0;i<n;i++){
  25. cin>>arr[i];
  26. sum+=arr[i];
  27. }
  28.  
  29. int low=0;
  30. int high = sum/k;
  31. int ans=-1;
  32.  
  33. while(low<=high){
  34. int mid=(low+high)/2;
  35.  
  36. if(check(arr,mid,k)==true){
  37. ans=mid;
  38. low=mid+1;
  39. }
  40. else{
  41. high=mid-1;
  42.  
  43. }
  44. }
  45. cout<<ans<<endl;
  46.  
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 544KB
stdin
3 2 
3 3 3 
stdout
4