C++ || Using Sorting + Map + Stack || Beats 100% || Easiest Code
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:\nO(n*log(n))\n\n- Space complexity:\no(n)\n\n# Code\n```\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        int i,j,n=items.size();\n        sort(items.rbegin(),items.rend());\n        map<int,int> mp;\n        stack<int> stk;\n        long long sum=0,ans=LLONG_MIN;\n        for(i=0;i<k;i++)\n        {\n            sum+=items[i][0];\n            if(mp.find(items[i][1])==mp.end())\n            {\n                mp[items[i][1]]=1;\n            }\n            else\n            {\n                stk.push(items[i][0]);\n            }\n        }\n        ans=max(ans,(long long)sum+(long long)mp.size()*(long long)mp.size());\n        for(i=k;i<n;i++)\n        {\n            if(mp.find(items[i][1])==mp.end()&&stk.size()>0)\n            {\n                sum-=stk.top();\n                stk.pop();\n                mp[items[i][1]]=1;\n                sum+=items[i][0];\n                ans=max(ans,(long long)sum+(long long)mp.size()*(long long)mp.size());                \n            }\n        }\n        return ans;          \n    }\n};\n```
['Stack', 'Ordered Map', 'Sorting', 'C++']