Maximize Sum of Pairs (fixed pairs)

VERY EASY · C · 2000ms

Team manager Zayan is picking cricket pairs for a fun exhibition match, and wants to pair up all players to MAXIMIZE the total skill-gap excitement across all pairs. Given 2N integers, pair them up into N pairs to MAXIMIZE the sum of |difference| across all pairs. Print the maximum total. (Hint: sort first.)

Input

Line1: 2N. Line2: 2N integers.

Output

Maximum total sum of pairwise absolute differences.

Constraints

1 ≤ N ≤ 5×10^4

Sample Input

4
1 5 3 8

Sample Output

9
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

Maximize Sum of Pairs (fixed pairs) · DIU ContestHub