C++ Greedy || sorting + map + priority Queue
\n# Code\n\nclass Solution {\npublic:    \n    long long findMaximumElegance(vector<vector<int>>&items, int k) {\n        sort(items.rbegin(),items.rend());\n  
\n# Code\n```\nclass Solution {\npublic:    \n    long long findMaximumElegance(vector<vector<int>>&items, int k) {\n        sort(items.rbegin(),items.rend());\n        long long sum=0;\n        map<int,multiset<int>>st;\n        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n        for(int i=0;i<k;i++){\n            sum+=items[i][0];\n            st[items[i][1]].insert(items[i][0]);\n        }\n        for(int i=1;i<=items.size();i++){\n            if(st.find(i)!=st.end() && st[i].size()>1) pq.push({*st[i].begin(),i});\n        }\n        long long size=st.size();\n        long long ans=sum+ size*size;\n        for(int i=k;i<items.size();i++){\n            if(pq.size()==0) break;\n            if(st.find(items[i][1])==st.end()){\n                int mini=pq.top().first;\n                int minid=pq.top().second;\n                pq.pop();\n                sum-=mini;\n                sum+=items[i][0];\n                st[items[i][1]].insert(items[i][0]);\n                st[minid].erase(st[minid].find(mini));\n                if(st[minid].size()==0) st.erase(minid);\n                if(st[minid].size()>1) pq.push({*st[minid].begin(),minid});\n                long long size=st.size();\n                ans=max(ans,sum + size*size);\n            }\n        }\n        return ans;\n    }\n};\n```
['Hash Table', 'Greedy', 'Sorting', 'Heap (Priority Queue)', 'Ordered Set', 'C++']