Beats 100 percent time
Intuition\n\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \n\n-
# Intuition\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& v, int k) {\n       sort(v.begin() , v.end());\n       reverse(v.begin() , v.end());\n       map<long long , long long> mp;\n       multiset<pair<long long , long long>> ms;\n       long long ans=0;\n       for(int i=0 ; i<k ;i++){\n           if(mp.find(v[i][1])!=mp.end()){\n               ms.insert({v[i][0] , v[i][1]});\n           }\n           mp[v[i][1]]++;\n    \n           ans += v[i][0];\n       }\n       ans += (pow(mp.size() , 2));\n       long long ans1 = ans;\n       int n = v.size();\n       for(int i=(k) ; i<n ;i++){\n           if(ms.size()==0)\n           break;\n           if(mp.find(v[i][1])!=mp.end())\n           continue;\n           else {\n               auto  h11 = ms.begin();\n               auto h1 = *h11;\n               long long h = h1.first;\n               long long h2 = h1.second;\n               long long l = mp.size();\n               auto j = (l+1)*(l+1);\n               auto j1 = l*l;\n                   ms.erase(h11);\n                   mp[h2]--;\n                   mp[v[i][1]]++;\n                   ans = ans-h-(l*l)+((l+1)*(l+1))+v[i][0];\n           }\n           ans1 = max(ans , ans1);\n       }\n       ans1 = max(ans , ans1);\n       return ans1;\n    }\n};\n```
['C++']