#include "solution.hpp"
using namespace std;
const int N=1e5+20;
int n,a[N];
long long dp[N];
class Solution {
public:
int solve(vector<int>& nums, int k) {
sort(nums.begin(),nums.end());
if(nums.empty()) return 0;
n=nums.size();
for(int i=1;i<=n;i++)
{
a[i]=nums[i-1];
dp[i]=dp[i-1]+a[i];
}
int ans=1,val=a[1];
for(int i=1;i<=n;i++)
{
int lo=i,hi=n,idx=-1;
while(lo<=hi)
{
int mid=(lo+hi)/2;
int mx=a[mid];
long long moves=1ll*mx*(mid-i+1)-(dp[mid]-dp[i-1]);
if(moves<=k) idx=mid,lo=mid+1;
else hi=mid-1;
}
if(ans<idx-i+1)
{
ans=idx-i+1;
val=a[idx];
}
}
return val;
}
};
I2luY2x1ZGUgInNvbHV0aW9uLmhwcCIKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBOPTFlNSsyMDsKCmludCBuLGFbTl07CmxvbmcgbG9uZyBkcFtOXTsKCmNsYXNzIFNvbHV0aW9uIHsKCXB1YmxpYzoKCWludCBzb2x2ZSh2ZWN0b3I8aW50PiYgbnVtcywgaW50IGspIHsKCSAgICBzb3J0KG51bXMuYmVnaW4oKSxudW1zLmVuZCgpKTsKCQlpZihudW1zLmVtcHR5KCkpIHJldHVybiAwOwoJCW49bnVtcy5zaXplKCk7CgkJZm9yKGludCBpPTE7aTw9bjtpKyspCgkJewoJCQlhW2ldPW51bXNbaS0xXTsKCQkJZHBbaV09ZHBbaS0xXSthW2ldOwoJCX0KCgkJaW50IGFucz0xLHZhbD1hWzFdOwoJCWZvcihpbnQgaT0xO2k8PW47aSsrKQoJCXsKCQkJaW50IGxvPWksaGk9bixpZHg9LTE7CgkJCXdoaWxlKGxvPD1oaSkKCQkJewoJCQkJaW50IG1pZD0obG8raGkpLzI7CgkJCQlpbnQgbXg9YVttaWRdOwoKCQkJCWxvbmcgbG9uZyBtb3Zlcz0xbGwqbXgqKG1pZC1pKzEpLShkcFttaWRdLWRwW2ktMV0pOwoJCQkJaWYobW92ZXM8PWspIGlkeD1taWQsbG89bWlkKzE7CgkJCQllbHNlIGhpPW1pZC0xOwoJCQl9CgkJCWlmKGFuczxpZHgtaSsxKQoJCQl7CgkJCSAgICBhbnM9aWR4LWkrMTsKCQkJICAgIHZhbD1hW2lkeF07CgkJCX0KCQl9CgkJcmV0dXJuIHZhbDsKCX0KfTs=