P1873 砍树
- 注意long long
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e6+10;
int n,m,h[maxn],ans;
bool check(int H){
long long temp=0;
for(int i=1;i<=n;i++){
if(h[i]>H) temp+=h[i]-H;
}
if(temp>=m) return true;
return false;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>h[i];
sort(h+1,h+1+n,greater<int>());//降序排序
int l=h[n],r=h[1];
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)){
ans=mid;
l=mid+1;
}
else{
r=mid-1;
}
}
cout<<ans<<endl;
}