weird sol but works
\n\n# Code\ncpp []\nclass Solution {\npublic:\n\n\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        unordered_set<int> cats;\n   
\n\n# Code\n```cpp []\nclass Solution {\npublic:\n\n\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        unordered_set<int> cats;\n        multimap<int, int, greater<int>> mp;\n        for(auto &x : items)\n        {\n            mp.insert({x[0], x[1]});\n            cats.insert(x[1]);\n        }\n        int cat_len = cats.size();\n        int tempk = k;\n        auto it = mp.begin();\n        unordered_map<int, int> seen, cnt;\n        int cycle = 1;\n        multiset<pair<long long, int>, greater<pair<long long, int>>> curr_set;\n        bool flag = false;\n        while(tempk > 0)\n        {\n            if(seen[it->second] && !flag)\n            {\n                auto check = next(it);\n                if(check == mp.end())\n                {\n                    it = mp.begin();\n                    flag = true;\n                }\n                else it = check;\n                continue;\n            }\n            seen[it->second] = 1;\n            curr_set.insert({it->first, it->second});\n            cnt[it->second]++;\n            it = mp.erase(it);\n            if(it == mp.end()){\n                flag = true;\n                it = mp.begin();\n            }\n            tempk--;\n        }\n\n        long long curr_sum = 0;\n        for(auto x : curr_set)curr_sum += x.first;\n        long long len = min((long long)cat_len , (long long)k);\n        long long res = curr_sum + len * len;\n\n        if(mp.size() == 0)return res;\n        it = mp.begin();\n        while(len > 0 && cnt[it->second] != 0 && cnt[it->second] < k && it != mp.end())\n        {\n            if(--cnt[prev(curr_set.end())->second] == 0)len--;\n            curr_sum = curr_sum - prev(curr_set.end())->first + it->first;\n            res = max(res, curr_sum + len * len);\n            curr_set.erase(prev(curr_set.end())); \n            it = mp.erase(it);\n        }\n        return res;\n    }\n};\n```
['C++']