C++ || Sorting || Map || Set
Approach\n1. Sort on the basis of profit in decreasing order.\n2. Now take first k elements and also store that which category occurs how many time.\n3. After k
# Approach\n1. Sort on the basis of profit in decreasing order.\n2. Now take first k elements and also store that which category occurs how many time.\n3. After k elements you will add any other number other than k elements if any category occurs more than one times in the k size window remove that element that add this current element into that k size and took maximum of all possibilities.\n# Complexity\n- Time complexity:\nnlogn\n\n# Code\n```\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        int n = items.size();\n        sort(items.rbegin(), items.rend());\n        long long ans = 0;\n        long long sum = 0;\n        map<int, int> mp;\n        multiset<pair<int, int>> st;\n        for(int i = 0; i < k; i++) {\n            sum += items[i][0];\n            mp[items[i][1]]++;\n            st.insert({items[i][0], items[i][1]});\n        }\n        sum += ((int) mp.size()) * 1LL * ((int) mp.size());\n        ans = max(ans, sum);\n        for(int i = k; i < n; i++) {\n            if(mp.find(items[i][1]) != mp.end()) {\n                continue;\n            }\n            while(st.size() > 0) {\n                auto it = *st.begin();\n                int profit = it.first;\n                int category = it.second;\n                if(mp[category] >= 2) {\n                    sum -= profit + ((int) mp.size()) * 1LL * ((int) mp.size());\n                    mp[items[i][1]]++;\n                    mp[category]--;\n                    sum += items[i][0] + ((int) mp.size()) * 1LL * ((int) mp.size());\n                    st.erase(st.begin());\n                    ans = max(ans, sum);\n                    break;\n                }\n                else {\n                    st.erase(st.begin());\n                }\n            }\n        }\n        return ans;\n    }\n};\n```
['Ordered Map', 'Sorting', 'Ordered Set', 'C++']