Just Sort and Take first K || Take remaining non Duplicates
Java []\nclass Solution {\n    public long findMaximumElegance(int[][] items, int k) {\n        Arrays.sort(items, (a,b)-> b[0]-a[0]);\n        Stack<Integer> d
```Java []\nclass Solution {\n    public long findMaximumElegance(int[][] items, int k) {\n        Arrays.sort(items, (a,b)-> b[0]-a[0]);\n        Stack<Integer> duplicates=new Stack<>();\n        HashSet<Integer> selected_cats=new HashSet<>(k);\n\n        long total_profit=0, elegance=0, size;\n        for(int i=0;i<items.length;i++) {\n            if(i<k){\n                total_profit+=items[i][0];\n                if(selected_cats.contains(items[i][1])) duplicates.add(items[i][0]);\n            } else {\n                if(duplicates.isEmpty()) break;\n                if(!selected_cats.contains(items[i][1]))\n                    total_profit+=items[i][0]-duplicates.pop();\n            }\n            selected_cats.add(items[i][1]);\n            elegance=Math.max(elegance, total_profit+(size=selected_cats.size())*size);\n        }\n        \n        return elegance;\n    }\n}\n```\nTC: $$O(N*logN)$$
['Stack', 'Ordered Set', 'Java']