C++ | greedy
\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 profitSum = 0, ans = 0, res = 0;\n        unordered_map<int,int> categoreis, dup;\n        for(int i = 0 ; i < k;i++){\n            profitSum += items[i][0];\n            int cat = items[i][1];\n            categoreis[cat]++;\n        }\n        ans = profitSum + 1LL*categoreis.size()*categoreis.size();\n        res = ans;\n        long long size = categoreis.size();\n        int j = k-1;\n        int n = items.size();\n        for(int i = k; i < n; i++){\n            int cat = items[i][1];\n            if(categoreis[cat] == 0){\n                while(j >= 0 && categoreis[items[j][1]] <= 1){\n                    j--;\n                }\n                if(j < 0){\n                    break;\n                }\n                categoreis[items[j][1]]--;\n                categoreis[cat]++;\n                res -= items[j][0];\n                res += items[i][0];\n                res -= size*size;\n                size++;\n                res += size*size;\n                j--;\n                ans = max(ans, res);\n            }\n        }\n        return ans;\n    \n    }\n};\n```
['Greedy', 'Sorting', 'C++']