【题解】
二分答案即可。
注意树的总高度会超过int,所以尽管M不超过int,check的时候还是要开Long Long,避免不必要的麻烦。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdlib> 5 #define LL long long 6 #define rg register 7 #define N 1000010 8 using namespace std; 9 LL n,m,l,r,mid,a[N]; 10 inline LL read(){ 11 LL k=0,f=1; char c=getchar(); 12 while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); 13 while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar(); 14 return k*f; 15 } 16 inline LL max(LL a,LL b){return a>b?a:b;} 17 inline bool check(){ 18 LL sum=0; 19 for(rg int i=1;i<=n;i++) sum+=max(0,a[i]-mid); 20 return sum>=m; 21 } 22 int main(){ 23 n=read(); m=read(); 24 for(rg int i=1;i<=n;i++) a[i]=read(),r=max(r,a[i]); 25 while(l+1<r){ 26 mid=(l+r)>>1; 27 if(check()) l=mid; 28 else r=mid; 29 } 30 printf("%lld\n",l); 31 return 0; 32 }