Sort the array and pair the smallest half with the largest half to maximize the sum of the smaller elements in each pair. Then sum the elements at even indices (0-based) after sorting, which represent the primary servers. This greedy strategy ensures each primary has a backup with at least as much memory while maximizing the total primary memory.
Pro tip: Always clarify that the primary servers are the ones with smaller memory in each pair, and that we want to maximize their sum. Mention that sorting and pairing smallest with largest is optimal, and you can prove it by an exchange argument.
Restate the problem: we have an even number of servers, need to form n/2 pairs where one is primary and one is backup, with backup memory >= primary memory. Maximize sum of primary memories.
Sort the array. Pair the smallest element with the largest, second smallest with second largest, etc. This ensures each primary (the smaller in each pair) has a backup at least as large.
After sorting, the primaries are the elements at even indices (0, 2, 4, ...). Sum these to get the maximum total primary memory.
Use an exchange argument: if any pairing is not smallest-with-largest, swapping to that configuration does not decrease the sum of primaries. Thus the greedy pairing is optimal.
Sorting takes O(n log n) time, and summing takes O(n) time, so overall O(n log n) time and O(1) extra space (if sorting in place).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.