#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3000+5;
const int M=10+5;
int n,m;
int p[N];
ll f[N][M],sum[N][N]={0};
inline int abss(int x){
return (x<0)?-x:x;
}
int main(){
freopen ("map.in","r",stdin);
freopen ("map.out","w",stdout);
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)scanf("%d",&p[i]);
sort(p+1,p+n+1);
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
sum[i][j]=sum[i][j-1]+abss(p[i]-p[j]);
}
}
memset(f,0x3f,sizeof(f));
f[0][0]=0;
for (int i=1;i<=n;i++){
f[i][1]=sum[(i+1)/2][i];
for (int j=2;j<=m;j++){
for (int k=j-1;k<i;k++){
f[i][j]=min(f[i][j],f[k][j-1]+sum[(i+k+1)/2][i]-sum[(i+k+1)/2][k]);
}
}
}
ll ans=f[0][1];
for (int i=1;i<=m;i++)ans=min(ans,f[n][i]);
printf("%lld\n",ans);
return 0;
}