#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=3030,INF=0x3f3f3f3f;
int V,P,X[N],dp[N][N],w[N][N],d[N][N];
void init()
{
for(int l=1; l<=V; l++)
{
w[l][l]=0;
for(int r=l+1; r<=V; r++)
w[l][r]=w[l][r-1]+X[r]-X[l+r>>1];
}
}
int main()
{
cin>>V>>P;
for(int i=1; i<=V; i++) cin>>X[i];
init();
sort(X+1,X+V+1);
memset(dp,0x3f,sizeof(dp));
dp[0][0]=0;
for(int j=1; j<=P; j++)
{
d[V+1][j]=V;
for(int i=V; i>=1; i--)
{
int minn=INF,minid;//w满足四边形不等式
for(int k=d[i][j-1]; k<=d[i+1][j]; k++)
{
if(dp[k][j-1]+w[k+1][i]<minn)
{
minn=dp[k][j-1]+w[k+1][i];
minid=k;
}
}
dp[i][j]=minn;
d[i][j]=minid;
}
}
cout<<dp[V][P]<<endl;
return 0;
}