Imagine you're a portfolio manager tasked with selecting exactly k items from a collection of investment opportunities. Each item has a profit and belongs to a specific category. Your goal is to maximize the elegance of your selection.
The elegance is calculated as: total_profit + (distinct_categories)ยฒ
This means you want to balance high profits with category diversity - having more distinct categories gives you a quadratic bonus!
Given a 2D array items where items[i] = [profit_i, category_i] and an integer k, find the maximum possible elegance from all subsequences of exactly k items.
Example: If you select items with profits [10, 15, 20] from categories [1, 2, 1], your elegance = 45 + 2ยฒ = 49 (total profit 45, plus 2 distinct categories squared).
Input & Output
Constraints
- 1 โค k โค items.length โค 105
- items[i].length == 2
- 1 โค profiti, categoryi โค 109
- All category values are distinct for different categories