1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #define MAX 2005
5 #define INF 2147483640
6
7 int DP[MAX][MAX / 2];
8 int S[MAX];
9
10 int fx(int n,int k);
11 int my_min(int i ,int j);
12 int comp(const void * a,const void * b);
13 int main(void)
14 {
15 int n,k;
16
17 while(scanf("%d%d",&n,&k) != EOF)
18 {
19 memset(DP,-1,sizeof(DP));
20
21 for(int i = 1;i <= n;i ++) //下标从1开始
22 scanf("%d",&S[i]);
23
24 qsort(&S[1],n,sizeof(S[0]),comp);
25 fx(n,k);
26 printf("%d\n",DP[n][k]);
27 }
28
29 return 0;
30 }
31
32 int fx(int n,int k)
33 {
34 if(DP[n][k] != -1)
35 return DP[n][k];
36
37 if(k == 0 || n == 0)
38 DP[n][k] = 0;
39 else if(n < 2 * k)
40 DP[n][k] = INF;
41 else if(n == 2 * k)
42 DP[n][k] = fx(n - 2,k - 1) + (S[n] - S[n - 1]) * (S[n] - S[n - 1]);
43 else
44 DP[n][k] = my_min(fx(n - 1,k),fx(n - 2,k - 1) + (S[n] - S[n - 1]) * (S[n] - S[n - 1]));
45
46 return DP[n][k];
47 }
48
49 int my_min(int i ,int j)
50 {
51 return i < j ? i : j;
52 }
53
54 int comp(const void * a,const void * b)
55 {
56 return *(int *)a - *(int *)b;
57 }