Python Hard
\nclass Solution:\n    def findMaximumElegance(self, items: List[List[int]], k: int) -> int:\n\n        categories = set()\n\n        count = 0\n\n        ans =
```\nclass Solution:\n    def findMaximumElegance(self, items: List[List[int]], k: int) -> int:\n\n        categories = set()\n\n        count = 0\n\n        ans = 0\n\n        total_profit = 0\n\n        items.sort(key = lambda x: -x[0])\n\n        h = []\n\n\n        for profit, c in items:\n\n            if count < k:\n                count += 1\n                total_profit += profit\n\n                if c not in categories:\n                    categories.add(c)\n\n                else:\n                    heapq.heappush(h, profit)\n\n            \n            else:\n                if h and c not in categories:\n                    total_profit -= heapq.heappop(h)\n                    total_profit += profit\n                    categories.add(c)\n\n            \n            ans = max(ans, total_profit + len(categories) ** 2)\n\n            \n        return ans\n\n\n        \n                \n            \n        \n```
['Python3']