|| CPP || 💡 Track the repeating using stack, Hashing very simple implementaion.
Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \nO(nlogn)\n- Space complexity:\n Add your space complexity here, e.g. O(n) \nO(n)\n# 
# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(nlogn)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n# Code\n```\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        // sort according to the non-increasing order of the profit.\n        sort(items.begin(), items.end(), []( const vector<int> &x, const vector<int>&y ){return x[0] >y[0];});\n        // stack to track repeating element\n        stack<int> st;\n        // map to track distinct element\n        map<int, long long> ans;\n// cur stores the running sum of profit,\n// mx stores the maximum elegence that can be achived using k elements .\n        long long cur= 0, mx= 0;\n        int n = items.size();\n        for(int i =0; i < k; i++)\n        {\n            if(ans.count(items[i][1]))\n            st.push(i);\n            ans[items[i][1]]+=1;\n            x1+= items[i][0];\n        }\n        mx = cur;\n        mx += ans.size()*ans.size();\n        // if answer contains duplicates, try to replace them \n        if(ans.size() != k)        \n            for(int i = k; i < n ; i++)\n            {\n                if(ans.count(items[i][1]))continue;\n                if(st.size() ==0 ) break;\n\n                auto last = st.top();\n                long long x =  ans.size() ;\n                    st.pop();\n                    // remove last repeating element and add fresh entry\n                    ans[items[last][1]]-=1;\n                    ans[items[i][1]]+=1 ;\n                    x1 -= items[last][0];\n                    x1 += items[i][0];\n                    // update the ans;\n                    mx  = max(x1 + (long long)(ans.size()*1LL*ans.size()) , mx);\n            }\n       return mx;\n    }\n};\n```
['Array', 'Hash Table', 'Stack', 'Greedy', 'Sorting', 'C++']