Not elegant, but effective
Code\n\nclass Solution:\n    def findMaximumElegance(self, items: List[List[int]], k: int) -> int:\n        cat = {}\n        hp = []\n        for val,c in item
# Code\n```\nclass Solution:\n    def findMaximumElegance(self, items: List[List[int]], k: int) -> int:\n        cat = {}\n        hp = []\n        for val,c in items:\n            if c not in cat:\n                cat[c] = val\n            elif cat[c] < val:\n                heappush(hp,-cat[c])\n                cat[c] = val\n            else:\n                heappush(hp,-val)\n        lis = sorted(cat.values(),reverse = True)\n        l = min(len(lis),k)\n        ans = sum(lis[:k]) + l ** 2\n        for _ in range(len(lis), k):\n            ans -= heappop(hp)\n        res = ans\n        while l and hp:\n            l -= 1\n            a, b = lis[l], -heappop(hp)\n            if a < b:\n                ans += b - a - 2 * l - 1\n                res = max(res,ans)\n            else:\n                break\n        return res\n                \n\n\n\n```
['Python3']