[C++] Group by category, add the most profitable and heap for the remaining
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    using integer = long long;\npublic:\n    integer findMaximumElegance(vector<vector<int>>& items, int k) {\n        std::map<int, std::vector<integer>> cat_to_profits;\n        for (const auto &item : items) {\n            cat_to_profits[item[1]].push_back(item[0]);\n        }\n        std::vector<std::vector<integer>> profits_by_cat;\n        for (auto &&[_, profits] : cat_to_profits) {\n            std::sort(profits.begin(), profits.end());\n            profits_by_cat.push_back(profits);\n        }\n        \n        // Indices to load profits by values\n        std::vector<int> cat_inds_ord(profits_by_cat.size());\n        std::iota(cat_inds_ord.begin(), cat_inds_ord.end(), 0);\n        std::sort(\n            cat_inds_ord.begin(), cat_inds_ord.end(), \n            [&](const int i, const int j) -> bool {\n                return profits_by_cat[i].back() > profits_by_cat[j].back();\n            });\n\n        std::priority_queue<integer, std::vector<integer>, std::greater<integer>> vals_ord;\n        int remaining = k;\n        integer max_sum = 0;\n        integer running_sum = 0;\n        const auto cleanse_vals_ord = [&]() {\n            while (vals_ord.size() > remaining) {\n                running_sum -= vals_ord.top();\n                vals_ord.pop();\n            }\n        };\n        for (int idx = 0; idx < cat_inds_ord.size(); ++idx) {\n            const int cat = cat_inds_ord[idx];\n            auto &&profits = profits_by_cat[cat];\n            const integer len = idx;            \n            running_sum += (profits.back() + (len + 1) * (len + 1) - len * len);            \n            remaining -= 1;\n            profits.pop_back();\n            cleanse_vals_ord();\n            for (; !profits.empty(); profits.pop_back()) {\n                vals_ord.push(profits.back());\n                running_sum += profits.back();\n                cleanse_vals_ord();\n            }\n            if (idx + 1 + vals_ord.size() == k) {\n                max_sum = std::max(running_sum, max_sum);\n            }\n        }\n        return max_sum;\n    }\n};\n```
['C++']