C++
\nusing LL = long long;\nusing PII = pair<LL, LL>;\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        s
```\nusing LL = long long;\nusing PII = pair<LL, LL>;\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        sort(items.rbegin(), items.rend());\n        LL sum = 0;\n        unordered_map<int, int>Map;  //[category, count]\n        for(int i = 0; i < k; i++){\n            sum += items[i][0];\n            Map[items[i][1]]++;\n        }\n        LL dist_cate = Map.size();\n        LL ret = sum + dist_cate*dist_cate;\n        cout << ret;\n        priority_queue<PII, vector<PII>, greater<>>pq;\n        for(int i = 0; i < k; i++){\n            pq.push({items[i][0], items[i][1]});\n        }\n        for(int i = k; i < items.size(); i++){\n            if(Map.find(items[i][1]) != Map.end())\n                continue;\n            while(!pq.empty()){\n                auto [profit, category] = pq.top();\n                pq.pop();\n                if(Map[category] > 1){\n                    sum -= profit;\n                    sum += items[i][0];\n                    dist_cate++;\n                    Map[category]--;\n                    Map[items[i][1]]++;\n                    ret = max(ret, sum + dist_cate*dist_cate);\n                    break;\n                }\n            }\n        }\n        return ret;\n    }\n};\n```
[]