Python, Hash Table, Sorting with comments
Intuition\nWe separate each item according to their categories.\nWe separete the largest of each category to their own list.\nWe sort the lists to find the smal
# Intuition\nWe separate each item according to their categories.\nWe separete the largest of each category to their own list.\nWe sort the lists to find the smallest items of each list\nWe compare the smallest values of each list to each other removing the smaller until we are left with k items. \nIn these comparisons the unique list will get a \'bonus\' to represent the change in number of categories.\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: (O)nlogn\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:(O)n\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfrom collections import defaultdict\n\nclass Solution:\n    def findMaximumElegance(self, items: List[List[int]], k: int) -> int:\n                \n        #Store items according to their categories\n        store = defaultdict(list)        \n        for val, category in items:\n            store[category].append(val)\n            \n        #Separate largest item of each category from the rest\n        unique = []\n        double = []        \n        for lista in store.values():\n            lista.sort()\n            unique.append(lista.pop())\n            double += lista\n            \n        #Delete smallest items until we are left with k items\n        unique = sorted(unique, reverse=True)[:k]\n        double = sorted(double, reverse=True)[:k-1]        \n        while len(unique) + len(double) > k:\n            if double[-1] < unique[-1] + len(unique) ** 2 - (len(unique) - 1) ** 2:\n                double.pop()\n            else:\n                unique.pop()\n        \n        return sum(unique) + sum(double) + len(unique) ** 2\n\n```
['Hash Table', 'Greedy', 'Sorting', 'Python3']