CodeForces - 1509C
Chthohepta · · 个人记录
题意
学生会正在为体育节上的接力赛做准备。
学生会共有
你需要最小化所有棒次差异值的总和。为此,你可以调整成员的参赛顺序。请问能达到的最小总和是多少?
思路
将
所以我们可以给排序的
代码
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
const int N = 2002;
int n, s[N];
ll dp[N][N];
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> s[i];
}
sort(s + 1, s + 1 + n);
for(int l = 1; l <= n; l ++){
for(int i = 1, j; i <= n; i ++){
j = i + l - 1;
if(j > n) break;
dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]) + s[j] - s[i];
}
}
cout << dp[1][n];
return 0;
}