fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. long long arr[1000050],n,m,ans=-1,maxx=0;
  4. bool check(int x){
  5. long long sum=0;
  6. for(int i=1;i<=n;i++){
  7. if(x<arr[i]){
  8. sum+=arr[i]-x;
  9. }
  10. }
  11. return sum>=m;
  12. }
  13. int main(){
  14. cin>>n>>m;
  15. for(int i=1;i<=n;i++){
  16. cin>>arr[i];
  17. maxx=max(maxx,arr[i]);
  18. }
  19. long long l=0,r=maxx;
  20. while(l<=r){
  21. int mid=(l+r)/2;
  22. if(check(mid)){
  23. ans=mid;
  24. l=mid+1;
  25. }else{
  26. r=mid-1;
  27. }
  28. }
  29. cout<<ans;
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5280KB
stdin
5 20
4 42 40 26 46
stdout
36