This question evaluates combinatorial optimization and constrained matching skills, focusing on algorithm design, complexity analysis, and resource-allocation reasoning for maximizing aggregate capacity under pairing constraints.
You are given n servers, where server i has memory capacity memory[i]. A valid system must contain an even number of servers.
If the system contains 2x servers, then:
x
servers are designated as
primary
x
servers are designated as
backup
P
must be paired with a
distinct
backup server
B
such that
memory[B] >= memory[P]
The system memory capacity is defined as the sum of memory capacities of all primary servers.
Task: Given n and the array memory, compute the maximum possible system memory capacity over all valid choices of an even-sized subset of servers and a valid primary/backup pairing.
Complete the function:
maximumCapacity(memory)
→ returns a 64-bit integer (long)
Constraints:
2 <= n <= 2 * 10^5
memory[i]
are integers (assume non-negative)
Notes:
n
servers (you do not have to use all servers).