Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) over all i is maximized.
Return the maximized sum.
Function Signature
def max_sum_of_pair_mins(nums: list[int]) -> int:
Rules
-
Every element belongs to exactly one pair. Equal values at different positions are separate elements and may be paired with each other.
-
Return only the sum, not the pairs.
Constraints
-
1 <= n <= 10^4
-
nums.length == 2 * n
-
-10^4 <= nums[i] <= 10^4
Examples
Input: nums = [1,4,3,2]
Output: 4
Sorted, the values are [1, 2, 3, 4]. The pairs (1, 2) and (3, 4) give 1 + 3 = 4, while the other two pairings give 1 + 2 = 3.
Input: nums = [6,2,6,5,1,2]
Output: 9
Sorted, the values are [1, 2, 2, 5, 6, 6]. The pairs (1, 2), (2, 5) and (6, 6) give 1 + 2 + 6 = 9.
Input: nums = [-1,-5,3,0]
Output: -5
The pairs (-5, -1) and (0, 3) give -5 + 0 = -5. The other pairings, (-5, 0) with (-1, 3) and (-5, 3) with (-1, 0), both give -6.