Heap/Priority_queue code with comments easy to understand.
Intuition\n\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(n)\n\n# Code
# Intuition\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n    def findMaximumElegance(self, items: List[List[int]], k: int) -> int:\n        n=len(items)\n        items = sorted(items, key=lambda x : -x[0])\n        t_sum=0\n        d={}\n        heap = []\n        #calculating for top k (top k profit, same category element maybe there).\n        for i in range(k): \n            t_sum+=items[i][0]\n            d[items[i][1]]=d.get(items[i][1],0)+1\n            heapq.heappush(heap, items[i])\n        ans=t_sum+len(d)**2\n\n        for i in range(k,n):\n            #a bigger profit element of same category is aready in heap.\n            if items[i][1] in d: \n                continue\n            #pop element which is only element of that catgory so dont  subtract it from t_sum because it may be part of soluition.\n            while heap and d[heap[0][1]]==1:\n                heappop(heap)\n            #multiple elements belongs to same category so we can remove least of them and add new category element to increate profit.\n            if heap:\n                profit,idx=heapq.heappop(heap)\n                d[idx]-=1\n                t_sum+=items[i][0]-profit\n                d[items[i][1]]=1\n                ans=max(ans,t_sum+len(d)**2)\n            # nothing to replace in heap so cant get beter then previous profits\n            else: \n                break\n        return ans\n```
['Heap (Priority Queue)', 'Python3']