AmazonOA #274
Maximum System Memory Capacity
Estimated EasyJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
Choose an even number of the n servers: half of them are primaries and half are backups. Each server is used only once, and every primary is paired with a backup whose capacity is at least its own. The contribution is the sum of all primary capacities; find its maximum. You do not have to use all servers.
Input
The first line contains n and the second line contains memory. 2≤n≤200000, 1≤memory[i]≤10^9. The original constraints mistakenly named the variable size, but their original side note clearly refers to memory; this site uses one name throughout.
Output
Output a single integer, the answer.
Example 1
Input:
4
1 2 1 2
Output:
3
Example 2
Input:
3
1 2 1
Output:
1
Example 3
Input:
5
2 4 3 1 2
Output:
5
Example notes
The answers are 3, 1 and 5 respectively. In the third example, pair 4 with 3 and 2 with 2; the primary capacity is 3+2=5, and 1 is left unused.
Editorial
Loading solution…