二分答案
#include<iostream> #include<algorithm> using namespace std; const int N=100010; #define LL long long LL a[N],b[N]; int main() { LL n,k; cin>>n>>k; for(int i=1;i<=n;i++)cin>>a[i]; if(n==1) { cout<<k+a[1]; return 0; } sort(a+1,a+1+n); LL sum=0; for(int i=1;i<n;i++) { sum=(a[i+1]-a[i])*i; if(k-sum>=0) { k-=sum; } else { if(i>k){ cout<<a[i]<<endl; return 0; } else { cout<<a[i]+k/i<<endl; return 0; } } } if(k>0){ if(k>=n){ cout<<a[n]+k/n<<endl; } else { cout<<a[n]<<endl; } } }