Easy Java Solution
\nclass Solution {\n    public long findMaximumElegance(int[][] items, int k) {\n        long ans = 0;\n        Arrays.sort(items, (a,b)-> b[0]-a[0]);\n        
```\nclass Solution {\n    public long findMaximumElegance(int[][] items, int k) {\n        long ans = 0;\n        Arrays.sort(items, (a,b)-> b[0]-a[0]);\n        Set<Integer> categoriesSeen = new HashSet<>();\n        List<Integer> duplicateCategories = new ArrayList<>();\n        for(int i=0;i<k;i++){\n            ans += items[i][0];\n            int currCat = items[i][1];\n            if(categoriesSeen.contains(currCat)){\n                duplicateCategories.add(items[i][0]);\n            }else{\n                categoriesSeen.add(currCat);\n            }\n        }\n        long curr = ans;\n        ans += 1L*categoriesSeen.size()*categoriesSeen.size();\n        \n        int n = items.length;\n        for(int i=k;i<n;i++){\n            int currCat = items[i][1];\n            int currVal = items[i][0];\n            \n            if(categoriesSeen.contains(currCat)){\n                continue;\n            }\n            if(duplicateCategories.size()==0){\n                break;\n            }\n            curr += currVal - duplicateCategories.get(duplicateCategories.size()-1);\n            duplicateCategories.remove(duplicateCategories.size()-1);\n            categoriesSeen.add(currCat);\n            ans = Math.max(ans, curr + 1L*categoriesSeen.size()*categoriesSeen.size());\n        }\n        return ans;\n    }\n}\n```
['Ordered Set', 'Java']