Two pointers + median solution
Code\n\n#define ll long long\n\nclass Solution {\npublic:\n    \n    ll getScore(int i, int j, vector<int>& nums, vector<ll>& pre){\n        int mid = i + (j-i)
# Code\n```\n#define ll long long\n\nclass Solution {\npublic:\n    \n    ll getScore(int i, int j, vector<int>& nums, vector<ll>& pre){\n        int mid = i + (j-i)/2;\n        ll val1 = (pre[j+1] + pre[i] - pre[mid+1] - pre[mid+1]);\n        ll val2 = (2*mid - j - i + 1) * (ll)nums[mid];\n        ll val  = val1+val2;\n        // cout<<i<<" "<<j<<" "<<val1<<" "<<val2<<" "<<val<<endl;\n        return val;\n    }\n    \n    int maxFrequencyScore(vector<int>& nums, long long k) {\n        sort(nums.begin(), nums.end());\n        int n = nums.size();\n        vector<ll> pre(n+1, 0);\n        \n        for(int i=0; i<n; i++){\n            pre[i+1] = pre[i] + (ll)nums[i];\n        }\n        \n        // for(int j=0; j<n; j++){\n        //     cout<<getScore(0, j, nums, pre)<<endl;\n        // }\n        \n        int i=0, j=0;\n        int val = 0;\n        int ans = 1;\n        while(j<n){\n            // cout<<i<<" "<<j<<" "<<getScore(i, j, nums, pre)<<endl;\n            while(j<n && getScore(i, j, nums, pre)<=k){\n                ans = max(ans, j-i+1);\n                j++;\n            }\n\n            while(j<n && i<j && getScore(i, j, nums, pre)>k) i++;\n        }\n        \n        return ans;\n        \n    }\n};\n```
['C++']