Greedy , take favourable items , well explained Solution✔️✔️✔️✔️
Approach\n1. The function first sorts the items vector in descending order based on profits. It maintains a set seen to track distinct categories and a vector d
# Approach\n1. The function first sorts the items vector in descending order based on profits. It maintains a set seen to track distinct categories and a vector dup to store duplicate profits encountered in the initial k elements.\n\n2. Then, the function iterates through the sorted items. For the first k elements, it updates the sum of profits and checks if any duplicate profits are present. For subsequent elements, it optimally replaces the duplicate profits in the sum with higher individual profits.\n\n3. The function calculates the current elegance as sum + seen.size() * seen.size() at each step and keeps track of the maximum elegance (res).\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> O(nlog(n))\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->O(n)\n\n# Code\n```\n#define ll long long\nclass Solution {\npublic:\n    \n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        ll n = size(items), sum = 0, res = 0;\n        vector<ll> dup;\n        unordered_set<ll> seen;\n        sort(items.begin(), items.end(), greater<vector<int>>());\n        for(int i = 0; i<n; ++i)\n        {\n            if(i<k)\n            {\n                if(seen.count(items[i][1])) dup.push_back(items[i][0]);\n                sum += items[i][0];\n            }\n            else if(seen.find(items[i][1]) == seen.end())\n            {\n                if(!dup.size()) break;\n                sum += items[i][0] - dup.back();\n                dup.pop_back();\n            }\n            seen.insert(items[i][1]);\n            res = fmax(res, sum + seen.size() * 1ll *seen.size());\n        }\n        return res;\n    }\n};\n```
['C++']