Sliding window
\nclass Solution:\n    def maxFrequencyScore(self, nums: List[int], k: int) -> int:\n        nums.sort()\n        n = len(nums)\n        right = mid0 = cost = 0
```\nclass Solution:\n    def maxFrequencyScore(self, nums: List[int], k: int) -> int:\n        nums.sort()\n        n = len(nums)\n        right = mid0 = cost = 0\n        ans = 1\n\n        def adjustment(left,right,mid0):\n            mid1 = (left + right) // 2\n            return (nums[mid1] - nums[mid0]) * (2*mid1 - (left + right)) + (nums[right] - nums[mid1])\n        \n        for left in range(n):\n            while right < n and cost + adjustment(left,right,mid0) <= k:\n                cost += adjustment(left,right,mid0)\n                mid0 = (left + right) // 2\n                right += 1\n            ans = max(ans, right - left)\n            if right == n:\n                break            \n            mid1 = mid0 + 1 if (right - left) % 2 == 0 else mid0\n            cost += (nums[mid1] - nums[mid0]) * (2*mid1 - (left + right)) - (nums[mid1] - nums[left])\n            mid0 = mid1\n        return ans \n```
['Python3']