You are given an integer array nodes holding the bandwidth capacities of network nodes, sorted in non-decreasing order. The length n of the array is even.
Split the nodes into n / 2 pairs so that every node belongs to exactly one pair. Each pair has a primary node and a secondary node, and the bandwidth contribution of a pair is the minimum of the two nodes' capacities.
Return the maximum total bandwidth contribution you can reach over all possible pairings.
Function Signature
def max_bandwidth(nodes: list[int]) -> int:
Rules
-
Every node is used in exactly one pair, so there are exactly n / 2 pairs.
-
Which node of a pair is the primary and which is the secondary does not change the pair's contribution.
-
Return the maximum total as an integer.
Constraints
-
2 <= len(nodes) <= 10^5
, and
len(nodes)
is even
-
1 <= nodes[i] <= 10^9
-
nodes
is sorted in non-decreasing order
Examples
Input: nodes = [1,2,3,4,5,6]
Output: 9
The pairing (1,2), (3,4), (5,6) contributes 1 + 3 + 5 = 9, and no pairing does better. The pairing (1,4), (2,5), (3,6), for comparison, contributes only 1 + 2 + 3 = 6.
Input: nodes = [1,1,1,1]
Output: 2
All capacities are equal, so every pairing contributes 1 + 1 = 2.
Input: nodes = [2,2,5,9,10,10]
Output: 17
The pairing (2,2), (5,9), (10,10) contributes 2 + 5 + 10 = 17. Splitting the two 10s, as in (2,10), (2,10), (5,9), drops the total to 2 + 2 + 5 = 9.