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