#include<cstdio>
#include<iomanip>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<unordered_map>
#define Quick ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define P pair<int,int>
#define inf 0x3f3f3f3f
#define endl '\n'
#define PI acos(-1)
#define mem(a, b) memset(a, b, sizeof a)
using namespace std;
const int N =2007;
const int mod = 1e9 + 7;
ll a[N];
ll dp[N][N];
int main()
{
    Quick;

    //重新排序使其在每一位上的（max-min）总和最小

    int n; cin >> n;
    for (int i = 0; i < n; i++)cin >> a[i];
    sort(a, a + n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            dp[i][j] = 1e12;//初始dp数组保留最大值。
        }
    }
    //初始化dp数组。
    dp[0][0] = a[n - 1] - a[0];
    //减小这个差值，可以删掉一个最小的或者最大的
    for (int i = 1; i < n; i++)
    {
        //只删除最大值i次
        dp[i][0] = dp[i - 1][0] + a[n - i - 1] - a[0]; //删除最大值从右边删除，那么删除之后当前最大值就是a[n-i-1];
        //只删除最小值i次
        dp[0][i] = dp[0][i - 1] + a[n - 1] - a[i];     //删除最小值从左边删除，那么删除之后当前最小值就是a[i]。
    }

    //接下来开始进行状态转移取最优。
    for (int i = 1; i < n; i++) {
        for (int j = 1; i + j < n; j++) {
            //i+j不得超过n次。
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[n - i - 1] - a[j]);//选择删除最大值
            dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[n - i - 1] - a[j]);//选择删除最小值
        }
    }
    ll res = 1e12;
    for (int i = 0; i < n; i++) {
        res = min(res, dp[i][n - i - 1]);
    }
    cout << res << endl;
}
### Prompt

In CPP, your task is to solve the following problem:

The student council is preparing for the relay race at the sports festival.

The council consists of n members. They will run one after the other in the race, the speed of member i is s_i. The discrepancy d_i of the i-th stage is the difference between the maximum and the minimum running speed among the first i members who ran. Formally, if a_i denotes the speed of the i-th member who participated in the race, then d_i = max(a_1, a_2, ..., a_i) - min(a_1, a_2, ..., a_i).

You want to minimize the sum of the discrepancies d_1 + d_2 + ... + d_n. To do this, you are allowed to change the order in which the members run. What is the minimum possible sum that can be achieved?

Input

The first line contains a single integer n (1 ≤ n ≤ 2000) — the number of members of the student council.

The second line contains n integers s_1, s_2, ..., s_n (1 ≤ s_i ≤ 10^9) – the running speeds of the members.

Output

Print a single integer — the minimum possible value of d_1 + d_2 + ... + d_n after choosing the order of the members.

Examples

Input


3
3 1 2


Output


3


Input


1
5


Output


0


Input


6
1 6 3 3 6 3


Output


11


Input


6
104 943872923 6589 889921234 1000000000 69


Output


2833800505

Note

In the first test case, we may choose to make the third member run first, followed by the first member, and finally the second. Thus a_1 = 2, a_2 = 3, and a_3 = 1. We have:

  * d_1 = max(2) - min(2) = 2 - 2 = 0. 
  * d_2 = max(2, 3) - min(2, 3) = 3 - 2 = 1. 
  * d_3 = max(2, 3, 1) - min(2, 3, 1) = 3 - 1 = 2. 



The resulting sum is d_1 + d_2 + d_3 = 0 + 1 + 2 = 3. It can be shown that it is impossible to achieve a smaller value.

In the second test case, the only possible rearrangement gives d_1 = 0, so the minimum possible result is 0.

### Response

```cpp
#include<cstdio>
#include<iomanip>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<unordered_map>
#define Quick ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define P pair<int,int>
#define inf 0x3f3f3f3f
#define endl '\n'
#define PI acos(-1)
#define mem(a, b) memset(a, b, sizeof a)
using namespace std;
const int N =2007;
const int mod = 1e9 + 7;
ll a[N];
ll dp[N][N];
int main()
{
    Quick;

    //重新排序使其在每一位上的（max-min）总和最小

    int n; cin >> n;
    for (int i = 0; i < n; i++)cin >> a[i];
    sort(a, a + n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            dp[i][j] = 1e12;//初始dp数组保留最大值。
        }
    }
    //初始化dp数组。
    dp[0][0] = a[n - 1] - a[0];
    //减小这个差值，可以删掉一个最小的或者最大的
    for (int i = 1; i < n; i++)
    {
        //只删除最大值i次
        dp[i][0] = dp[i - 1][0] + a[n - i - 1] - a[0]; //删除最大值从右边删除，那么删除之后当前最大值就是a[n-i-1];
        //只删除最小值i次
        dp[0][i] = dp[0][i - 1] + a[n - 1] - a[i];     //删除最小值从左边删除，那么删除之后当前最小值就是a[i]。
    }

    //接下来开始进行状态转移取最优。
    for (int i = 1; i < n; i++) {
        for (int j = 1; i + j < n; j++) {
            //i+j不得超过n次。
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[n - i - 1] - a[j]);//选择删除最大值
            dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[n - i - 1] - a[j]);//选择删除最小值
        }
    }
    ll res = 1e12;
    for (int i = 0; i < n; i++) {
        res = min(res, dp[i][n - i - 1]);
    }
    cout << res << endl;
}
```