Binary Search + Sliding window
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    using ll = long long;\n    int maxFrequencyScore(vector<int>& a, long long k) {\n        int n = a.size();\n        sort(a.begin(), a.end());\n        vector<ll> pre(n+1, 0);\n        for (int i = 0; i < n; i++) pre[i+1] = pre[i] + a[i]*1LL;\n        int ans = 1;\n        int l = 0, r = n;\n        function<bool(int)> valid = [&](int len) {\n            int start = 0;\n            for (int i = 0; i < n; i++) {\n                if (i-start +1 == len) {\n                    int middle = (start+i)/2;\n                    ll before = (middle-start+1)*1LL*a[middle]*1LL -  (pre[middle+1] - pre[start]);\n                    ll after =  (pre[i+1] - pre[middle]) - (i-middle+1)*1LL*a[middle]*1LL;\n                    if (before + after <= k) return true;\n                    start++;\n                }\n            }\n            return false;\n        };\n        while (l<=r) {\n            int mid = (l+r)/2;\n            if (valid(mid)) {\n                ans = mid;\n                l = mid+1;\n            } else {\n                r = mid - 1;\n            }\n        }\n        return ans;\n    }\n};\n```
['Binary Search', 'Sliding Window', 'C++']