C++ Easy Solution || Sort by Profit || Commented code
Intuition\n Describe your first thoughts on how to solve this problem. \nSort items based on decreasing order of profits and take k elements which leads to maxi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSort items based on decreasing order of profits and take k elements which leads to maximize elegance.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        unordered_set<int> s;\n        sort(items.begin(), items.end(), [](const vector<int> &A, const vector<int> &B){\n            return A[0] > B[0];\n        });\n\n        long long curr = 0, max_res = 0;\n        vector<int> duplicate;\n        int n = items.size();\n\n        for(int i = 0; i < n; i++)\n        {\n            if (i < k) // Set is still not reached it\'s capacity, we can take the element\n            {\n                if(s.find(items[i][1]) != s.end()) // we haven seen this category previously, thus updating duplicate array,\n                                                    // this dup array will ensure that if in some later stage we need to remove a \n                                                    // duplicate element, we remove the element that has lowest profit.\n                                                    // we may need to remove multiple such duplicates thus need to maintain an array\n                                                    // instead of some variable that holds last duplicate value.\n                {\n                    duplicate.push_back(items[i][0]);\n                }\n                curr += items[i][0];\n                s.insert(items[i][1]);                \n            }\n            else    // We already had reached k elements, we can\'t add a new element without removing an existing element.\n            {\n                \n                if (s.find(items[i][1]) == s.end()) // This is an unique category element, we need to remove a duplicate element, whose profit\n                                                    // is the last element of duplicate array and calculate elegance\n                {\n                    if (duplicate.size() == 0) // No duplicate element found, need to break from the for loop\n                        break;\n                    curr = curr - duplicate.back(); // Removing the profit from duplicate and subtracting from current profit\n                    duplicate.pop_back();       \n                    curr = curr + items[i][0];      // Adding item profit to the current profit.\n                    \n                }\n                s.insert(items[i][1]);          // Adding the category to the set s.\n\n                // But if the category is already been seeing then we can ignore the element\n                // since we have an element with same category previously and this element can\'t contribute to maximize the ans.\n                // And we can\'t take this element since we already has k elements in the resultant array. \n            }\n\n            //s.insert(items[i][1]);\n            max_res = fmax(max_res, curr + s.size() * s.size());    // calculate elegance\n        }\n\n        return max_res;\n    }\n};\n```
['Hash Table', 'Sorting', 'C++']