Python 3 || Explained in detail
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\nAs we know we finally
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nAs we know we finally want to return a sum of all the first elements in an array and square of different number of categories that are the number of different elements in the array. So write an function that takes the input array and its hashmap of the frequency of the second element in the array.\n\n\nFirst of all sort the items in descending order and create an candidate set that has top k elements.\n\nCreate a hashmap that stores the the frequency of different categories.\n\nIf the length of the hashmap is equal to the length of the candidate set that means all the elements of the candidate set gave unique categories. Anyways we have all the elements having highest value. Thus there cant be any higher elegance value. So return it .\n\nElse find the elegance initially. For all the remaining elements in the original array, we keep on replacing the minimum element in the candidate set having a repeated category with the array element.After every replacement , calculate the elegance and if the new value is higher than the previous then update the elegance value. After all the iterations return the elegance.\n\n# Code\n```\nclass Solution:\n    def findMaximumElegance(self, items: List[List[int]], k: int) -> int:\n        \n        def find_elegance(arr, freq)->int:\n            sum = 0\n            for i in arr:\n                sum+=i[0]\n\n            return sum + len(freq.keys())**2\n\n        # items = [[1,1],[2,1],[3,1]]\n        # k = 3\n\n        # sum = 0\n\n\n        items.sort(reverse = True)\n\n        candidate_set = items[:k]\n        items = items[k:]\n\n        # print(candidate_set)\n        # print(items)\n\n        freq = {}\n\n        for i in candidate_set:\n            if i[1] in freq:\n                freq[i[1]]+=1\n            else:\n                freq[i[1]] = 1\n\n        found = False\n\n        # print(freq)\n\n        if len(freq.values()) == len(candidate_set):\n            return(find_elegance(candidate_set, freq))\n\n        else:\n\n            ini_elegance= find_elegance(candidate_set, freq)\n            # print(candidate_set, items, sep = "|")\n            for i in items:\n                if i[1] not in freq:\n                    for j in range(len(candidate_set)-1, -1, -1):\n                        # print(j)\n                        if freq[candidate_set[j][1]]>1:\n                            freq[candidate_set[j][1]] -=1\n                            candidate_set[j] = i\n                            if candidate_set[j][1] in freq:\n                                freq[candidate_set[j][1]] +=1\n                            else:\n                                freq[candidate_set[j][1]] = 1\n                            # print(freq)\n                            break\n                    ini_elegance = max(ini_elegance, find_elegance(candidate_set, freq))\n\n            return(ini_elegance)\n\n\n\n```
['Python3']