The student council is preparing for the relay race at the sports festival.
The council consists of n n n members. They will run one after the other in the race, the speed of member i i i is s i s_i si. The discrepancy d i d_i di of the i i i-th stage is the difference between the maximum and the minimum running speed among the first i i i members who ran. Formally, if a i a_i ai denotes the speed of the i i i-th member who participated in the race, then d i = m a x ( a 1 , a 2 , … , a i ) − m i n ( a 1 , a 2 , … , a i ) d_i=max(a_1,a_2,…,a_i)−min(a_1,a_2,…,a_i) di=max(a1,a2,…,ai)−min(a1,a2,…,ai).
You want to minimize the sum of the discrepancies d 1 + d 2 + ⋯ + d n d_1+d_2+⋯+d_n d1+d2+⋯+dn. 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 ) n (1≤n≤2000) n(1≤n≤2000) — the number of members of the student council.
The second line contains n n n integers s 1 , s 2 , … , s n ( 1 ≤ s i ≤ 1 0 9 ) s_1,s_2,…,s_n (1≤s_i≤10^9) s1,s2,…,sn(1≤si≤109) – the running speeds of the members.
Output
Print a single integer — the minimum possible value of d 1 + d 2 + ⋯ + d n d_1+d_2+⋯+d_n d1+d2+⋯+dn 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 a_1=2, a_2=3 a1=2,a2=3, and a 3 = 1 a_3=1 a3=1. We have:
d
1
d_1
d1=max(2)−min(2)=2−2=0.
d
2
d_2
d2=max(2,3)−min(2,3)=3−2=1.
d
3
d_3
d3=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
d_1+d_2+d_3=0+1+2=3
d1+d2+d3=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 d_1=0 d1=0, so the minimum possible result is 0 0 0.
思路
对输入的数组进行排序。
首先我们容易得到一个结论:在最优的安排下,取出的数字在排序后的数组中,一定是一个连续的区间。
举一个小栗子,
[
1
,
2
,
3
,
4
,
5
]
[1,2,3,4,5]
[1,2,3,4,5] 这么五个元素,假如取出的顺序是
3
,
5
,
4...
3,5,4...
3,5,4... 。在取出
3
,
5
3,5
3,5 的时候,数字的下标不连续,那么它必不是最优解。至少
3
,
4
,
5
3,4,5
3,4,5 就优于
3
,
5
,
4
3,5,4
3,5,4。
有了这个结论后,就可以推状态转移了。
设 w [ i ] [ j ] w[i][j] w[i][j] 代表从 i i i 到 j j j 这两个下标之间的数字的极差,显然 w [ i ] [ j ] = a [ j ] − a [ i ] w[i][j]=a[j]-a[i] w[i][j]=a[j]−a[i]。
设 d p [ i ] [ j ] dp[i][j] dp[i][j] 代表 [ i , j ] [i,j] [i,j] 这个区间内的最小答案, [ i , j ] [i,j] [i,j] 这个区间只能从 [ i + 1 , j ] [i+1,j] [i+1,j]、 [ i , j − 1 ] [i,j-1] [i,j−1] 两个区间转移而来,所以 d p [ i ] [ j ] = m i n ( d p [ i + 1 ] [ j ] , d p [ i ] [ j − 1 ] ) + w [ i ] [ j ] dp[i][j]=min(dp[i+1][j],dp[i][j-1])+w[i][j] dp[i][j]=min(dp[i+1][j],dp[i][j−1])+w[i][j].
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2010;
int n,a[N],dp[N][N];
signed main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1);
for(int len=2;len<=n;len++)
for(int i=1;i<=n;i++){
int j=i+len-1;
dp[i][j]=min(dp[i+1][j],dp[i][j-1])+a[j]-a[i];
}
cout<<dp[1][n]<<"\n";
}