C++| Priority Queue Solution
Code\n\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        int n = items.size();\n        sort(items.beg
# Code\n```\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        int n = items.size();\n        sort(items.begin(), items.end(), greater<vector<int>>{}); // greater profit to least\n\n        long long distinct = 0, total = 0;\n        vector<int> count(n+1, 0);\n        priority_queue<pair<int, int>> pq;\n\n        for(int i=0;i<k;i++) {\n            total += items[i][0];\n            pq.push({-items[i][0], items[i][1]});\n            if(!count[items[i][1]]++) distinct++;\n        }\n        long long ans = total + (distinct * distinct);\n        for(int i=k;i<n;i++) {\n            if(count[items[i][1]]) continue;\n            while(!pq.empty() and count[pq.top().second] == 1) pq.pop();\n            if(pq.empty()) break; // we have top k profits\n\n            total += (items[i][0] + pq.top().first);\n            distinct++;\n            ans = max(ans, total + (distinct * distinct));\n            count[pq.top().second]--;\n            pq.pop();\n            count[items[i][1]]++;\n            pq.push({-items[i][0], items[i][1]});\n        }\n        return ans;\n    }\n};\n\n```
['Sorting', 'Heap (Priority Queue)', 'Counting', 'C++']