Java
Intuition\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 
# Intuition\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:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n        public long findMaximumElegance(int[][] A, int k) {\n        Arrays.sort(A, (a, b) -> b[0] - a[0]);\n        long res = 0, cur = 0;\n        List<Integer> dup = new ArrayList<>();\n        Set<Integer> seen = new HashSet<>();\n        for (int i = 0; i < A.length; ++i) {\n            if (i < k) {\n                if (seen.contains(A[i][1])) {\n                    dup.add(A[i][0]);\n                }\n                cur += A[i][0];\n            } else if (!seen.contains(A[i][1])) {\n                if (dup.isEmpty()) break;\n                cur += A[i][0] - dup.remove(dup.size() - 1);\n            }\n            seen.add(A[i][1]);\n            res = Math.max(res, cur + 1L * seen.size() * seen.size());\n        }\n        return res;\n    }\n}\n```
['Java']