CF1509C.The Sports Festival
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 si . The discrepancy di 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 ai denotes the speed of the i -th member who participated in the race, then di=max(a1,a2,…,ai)−min(a1,a2,…,ai) .
You want to minimize the sum of the discrepancies 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?
输入格式
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 s1,s2,…,sn ( 1≤si≤109 ) – the running speeds of the members.
输出格式
Print a single integer — the minimum possible value of d1+d2+⋯+dn after choosing the order of the members.
输入输出样例
输入#1
3 3 1 2
输出#1
3
输入#2
1 5
输出#2
0
输入#3
6 1 6 3 3 6 3
输出#3
11
输入#4
6 104 943872923 6589 889921234 1000000000 69
输出#4
2833800505
说明/提示
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 a1=2 , a2=3 , and a3=1 . We have:
- d1=max(2)−min(2)=2−2=0 .
- d2=max(2,3)−min(2,3)=3−2=1 .
- d3=max(2,3,1)−min(2,3,1)=3−1=2 .
The resulting sum is 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 d1=0 , so the minimum possible result is 0 .