King and Collection of Taxes
EasyP3Coding
The king has the tax amounts of 2N citizens. He splits them into N groups of two, and from each pair he collects the larger of the two amounts. Return the largest total he can collect, over all ways to form the pairs.
(The original statement was ambiguous about the pairing rule; this version fixes it as "collect the larger amount of each pair, maximise the total".)
Example 1
- Input
- taxes = [1,2,3,4]
- Output
- 7
- Why
- Pair (1,3) and (2,4): collect 3 + 4. Or (1,2) and (3,4): 2 + 4 = 6. The best is 7.
Example 2
- Input
- taxes = [5,5]
- Output
- 5
Constraints
- 2 <= taxes.length <= 2 * 10^5, and the length is even
- 0 <= taxes[i] <= 10^9
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.