✅C++ Intuitive solution || ✍️Commented code ||
\n# Code\n\nclass Solution {\npublic:\n    bool static cmp(vector<int> &v1,vector<int> &v2){\n        return v1[0]>v2[0];\n    }\n    \n    long long findMaximu
\n# Code\n```\nclass Solution {\npublic:\n    bool static cmp(vector<int> &v1,vector<int> &v2){\n        return v1[0]>v2[0];\n    }\n    \n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        int n=items.size();\n\n        //stores initially taken items\n        priority_queue<pair<long long,long long>> pq;\n\n        //keeps track of number of categories\n        unordered_map<long long,long long> mp;\n\n        // sorting array\n        sort(items.begin(),items.end(),cmp);\n\n        long long ans=0;\n\n        //pushing first k items\n        for(int i=0 ; i<k ; i++){\n            ans+=items[i][0];\n            pq.push({-items[i][0],i});\n            mp[items[i][1]]++;\n        }\n        //initial res\n        long long res=ans+(long long)mp.size()*(long long)mp.size();\n\n        //checking for leftover items if it gives a better result\n        int ptr=k;\n        while(ptr<n && !pq.empty()){\n            //if item category already exists then continue\n            if(mp[items[ptr][1]]>0){\n                ptr++;\n                continue;\n            }\n            int curr=pq.top().second;\n            pq.pop();\n            //we cannot remove item if removing item removes existence of a category\n            if(mp[items[curr][1]]==1){\n                continue;\n            }\n\n            //we get a new category \n            ans+=items[ptr][0]-items[curr][0];\n            --mp[items[curr][1]];\n            ++mp[items[ptr][1]];\n            res=max(res,ans+(long long)mp.size()*(long long)mp.size());\n        }\n        return res;\n    }\n};\n```
['Sorting', 'C++']