Took me a minute to see the right move here.
Sort the array and use a greedy two-pointer strategy: pair the largest available server as a backup with the largest possible primary that it can cover, ensuring each primary is as large as possible. This maximizes the sum of primaries because each backup can only cover one primary, and we want to assign the largest backups to the largest primaries they can support.
Pro tip: Clarify that the goal is to maximize the sum of primary memories, not the number of pairs. Mention that if the array length is odd, one server will be left unpaired, and it's optimal to leave the smallest server unpaired.
Restate the problem: pair each primary with a backup such that backup >= primary, each server used once, maximize sum of primaries. Note that the array can be sorted without loss of generality.
Sort the memory values in non-decreasing order. This allows efficient pairing using two pointers or binary search.
Use two pointers: one at the end (largest) for backup, one at the middle for primary. For each backup from largest to smallest, find the largest primary that is <= backup and not yet paired. Pair them and move pointers accordingly.
If the array length is odd, leave the smallest element unpaired. Also handle cases where no valid pairing exists (e.g., all elements equal but odd count).
Sum the memories of all chosen primary servers and return the total. Verify with examples.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.