C++ greedy
\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        sort(items.begin(), items.end(), [&](const vector<i
```\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        sort(items.begin(), items.end(), [&](const vector<int>& a, const vector<int>& b) {\n            return a[0] > b[0];\n        });\n\n        unordered_set<int> m;\n        stack<int> s;\n        long long maxElegance = 0, sum = 0;\n        for (int i = 0; i < items.size(); ++i) {\n            auto profit = items[i][0], category = items[i][1];\n            if (i < k) {\n                sum += profit;\n                if (!m.insert(category).second) {\n                    s.push(i);\n                }\n                maxElegance = sum + m.size() * m.size();\n            } else if (!m.count(category) && !s.empty()) {       \n                auto p = items[s.top()][0], c = items[s.top()][1];\n                long long e = sum - p + profit + (m.size() + 1) * (m.size() + 1);\n                s.pop();\n                sum = sum - p + profit;\n                maxElegance = max(e, maxElegance);\n                m.insert(category);\n            }\n        }\n        return maxElegance;\n    }\n};\n```
[]