Solution in Swift
Intuition\nThe approach involves sorting the "items" array in descending order based on the "profiti". By selecting the first "k" items, we ensure that we attai
# Intuition\nThe approach involves sorting the "items" array in descending order based on the "profiti". By selecting the first "k" items, we ensure that we attain the highest possible "total_profit".\n\n# Approach\nUpon the selection of the initial "k" items, attention turns to the remaining "n - k" items. The viability of adding these items depends on whether they belong to an unexplored category (not yet in the "seen" set).\n\nGiven the restriction of maintaining a subsequence size of "k", a pivotal decision arises. To optimize the elegance metric, the algorithm strategically replaces an existing item with the lowest profit when that item shares its category with another.\n\nThis iterative refinement process continually adjusts the subsequence while upholding the imperative of category distinctiveness.\n\nThe final output of the function encapsulates the pinnacle of elegance attained through this intricate process\u2014a union of the cumulative impact of total profit and the singularity of categories.\n\n# Complexity\n- Time complexity: `O(nlogn)`\n- Space complexity: `O(n)`\n\n# Code\n```\nclass Solution {\n    func findMaximumElegance(_ items: [[Int]], _ k: Int) -> Int {\n        var items = items.sorted(by: { $0[0] > $1[0] })\n        var res: Int64 = 0\n        var cur: Int64 = 0\n        var dup = [Int]()\n        var seen = Set<Int>()\n\n        for i in 0..<items.count {\n            if i < k {\n                if seen.contains(items[i][1]) {\n                    dup.append(items[i][0])\n                }\n                cur += Int64(items[i][0])\n            } else if !seen.contains(items[i][1]) {\n                if dup.isEmpty {\n                    break\n                }\n                cur += Int64(items[i][0] - dup.removeLast())\n            }\n            seen.insert(items[i][1])\n            res = max(res, cur + Int64(seen.count) * Int64(seen.count))\n        }\n\n        return Int(res)\n    }\n}\n```
['Swift']