Under construction

Still being built. Things may change

Group Project

Interview

Time limit1sMemory limit512 MB

Summary
Split N students with distinct IDs into pairs and one triple so that the sum of each team's max-min ID gap is minimized.
Level

Medium5 of 10

Topics
Sorting, Dynamic programming
Solved
No attempts yet

Problem

A class of NN students must be split into teams for a group project. Every student must belong to exactly one team. A team should normally have 2 members, but since NN is odd, exactly one team has 3 members. The other N−32\frac{N-3}{2} teams have 2 members each.

A successful group project needs a friendly atmosphere. Each student has a unique student ID. The awkwardness of a team is the difference between the largest and smallest student IDs in that team.

Form the teams so that the sum of awkwardness over all teams is minimized.

Input

The first line contains the number of students NN. (3≤N<500 0003 \le N < 500\,000, NN is odd)

The second line contains NN integers A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N, separated by spaces, where AiA_i is the student ID of the ii-th student. (1≤Ai≤1091 \le A_i \le 10^9) All AiA_i are distinct.

Output

Print the minimum possible sum of awkwardness over all formed teams.

Examples2

  1. Example 1

    Input
    5
    3 1 4 5 9
    
    Expected output
    7
    
  2. Example 2

    Input
    7
    20 10 9 11 3 18 1
    
    Expected output
    6