Python Hard
\nclass Solution:\n    def maxFrequencyScore(self, nums: List[int], k: int) -> int:\n        nums.sort()\n        N = len(nums)\n        pref = [0] + list(accum
```\nclass Solution:\n    def maxFrequencyScore(self, nums: List[int], k: int) -> int:\n        nums.sort()\n        N = len(nums)\n        pref = [0] + list(accumulate(nums))\n        ans = 0\n        l = 0\n        for r in range(1, N + 1):\n            mid = (l + r) // 2\n            median = nums[mid]\n            cost = abs((r - mid) * median - (pref[r] - pref[mid]))\n            cost += abs((mid - l) * median - (pref[mid] - pref[l]))\n       \n            if cost > k:\n                l += 1\n\n            ans = max(ans, r - l)\n\n        return ans\n        \n\n```
['Python3']