greedy Sorting + heap + hashtable python3
\n\n# Code\n\nclass Solution:\n    def findMaximumElegance(self, items: List[List[int]], k: int) -> int:\n        items.sort(reverse = True)\n        items = [t
\n\n# Code\n```\nclass Solution:\n    def findMaximumElegance(self, items: List[List[int]], k: int) -> int:\n        items.sort(reverse = True)\n        items = [tuple(t) for t in items]\n        freq = {}\n        heap = []\n        sumpf = 0\n        categories = 0\n        res = 0\n        for i,(p,c) in enumerate(items):\n            if i<k:\n                if c not in freq:\n                    freq[c] = 0\n                    categories += 1\n                freq[c] += 1\n                sumpf += p\n                if freq[c] > 1:\n                    heapq.heappush(heap,(p,c))\n            if i >= k:\n                if c in freq: continue\n                while heap and freq[heap[0][1]] <= 1:\n                    heapq.heappop(heap)\n                if heap:\n                    topp,topc = heapq.heappop(heap)\n                    freq[topc] -= 1\n                    freq[c] = 1\n                    categories += 1\n                    sumpf += p - topp\n            if i >= k-1:\n                res = max(res,sumpf+categories*categories)\n        return res\n\n\n\n\n\n                \n                \n            \n            \n```
['Hash Table', 'Greedy', 'Sorting', 'Heap (Priority Queue)', 'Python3']