// sort
// dp[i][j] -> pre i thing choose j pair
// dp[i][j] = min(dp[i-1][j], dp[i-2][j-1]+cost(i, i-1));
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 2001
#define MAXPAIR 1001
#define INF 0x3f3f3f3f
long dp[MAXN][MAXPAIR];
int val[MAXN];
inline int cmp(const int &a, const int &b)
{
return a < b;
}
inline int cost(int x, int y)
{
return (x-y)*(x-y);
}
long dynamic_programming(int n, int pair)
{
long t_val;
for(int i = 0; i <= n; i ++) {
fill(dp[i], dp[i]+pair+1, INF);
}
for(int i = 2; i <= n; i ++) {
for(int j = 1; j <= pair && j*2 <= i; j ++) {
t_val = (INF == dp[i-2][j-1])? 0 : dp[i-2][j-1];
dp[i][j] = min(dp[i-1][j], t_val+cost(val[i-1], val[i]));
}
}
return dp[n][pair];
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int n, pair;
while( ~scanf("%d %d", &n, &pair) ) {
for(int i = 1; i <= n; i ++) {
scanf("%d", &val[i]);
}
sort(val+1, val+n+1, cmp);
printf("%ld\n", dynamic_programming(n, pair));
}
return 0;
}