C++ Greedy in O(NlogN)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nTaking k elements and trying to maximise the answer by increasing unique 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nTaking k elements and trying to maximise the answer by increasing unique elements\n\n# Complexity\n- Time complexity:\n- O(NlogN + N) for sorting and searching\n\n- Space complexity:\n- O(N)\n\n# Code\n```\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& v, int k) {\n        int n = v.size();\n        sort(v.rbegin(), v.rend()); //sort according to profit\n        long long ans = 0;\n        map<int, int> m;\n        for(int i = 0; i < k; i++){\n            ans += v[i][0];\n            m[v[i][1]]++;\n        }\n        long long sz = m.size();\n        ans += (sz * sz);\n        long long res = ans;\n        int j = k - 1;\n        for(int i = k; i < n; i++){\n            if(m.count(v[i][1]) == 0){ //try to increase unique elements\n                while(j >= 0 && m[v[j][1]] < 2) j--; //finding smallest number in 0 to k-1 whose frequency is more than 1, and removing it to increasing uniquenes of the subsequence\n                if(j < 0) break; //no number found that has frequency more than two\n                m[v[j][1]]--;\n                m[v[i][1]]++; \n                res -= v[j][0];\n                res += v[i][0];\n                res -= sz*sz;\n                sz++;\n                res += sz*sz;\n                j--;\n                ans = max(ans, res); //keep taking max\n            }\n        }\n        return max(ans, res);\n    }\n};\n```
['C++']