Priority_Queue | C++ | Sorting
Code\n\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        sort(items.begin(),items.end());\n        rev
# Code\n```\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        sort(items.begin(),items.end());\n        reverse(items.begin(),items.end());\n        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>p;\n        if(k==1){\n            return items[0][0]+1;\n        }\n        long long ans=0,sum=0;\n        map<int,int>m;\n        for(int i=0; i<k; i++){\n            p.push({items[i][0],items[i][1]});\n            sum+=items[i][0];\n            m[items[i][1]]++;\n            ans=max(ans,sum+(long long)m.size()*(long long)m.size());\n        }\n        for(int i=k; i<items.size(); i++){\n            if(m.find(items[i][1])!=m.end()) continue;\n            while(p.size() && m[p.top().second]==1){\n                p.pop();\n            }\n            if(p.empty()) break;\n            sum-=p.top().first;\n            m[p.top().second]--;\n            if(m[p.top().second]==0) m.erase(p.top().second);\n            p.pop();\n            sum+=items[i][0];\n            p.push({items[i][0],items[i][1]});\n            m[items[i][1]]++;\n            ans=max(ans,sum+(long long)m.size()*(long long)m.size());\n        }\n        return ans;\n    }\n};\n```
['Heap (Priority Queue)', 'C++']