/* It can be said that it is a problem with the backpack: the size of the backpack is K groups. K groups from N must be adjacent to two groups due to the smallest square difference. There are n-1 groups in total, finding the square difference between each group changes to the number of K hops and the smallest number of N-1 records (it seems simple, but you cannot move the same item twice, when we calculate the square difference between two adjacent numbers, except the first and the last, the other items are calculated twice.) Therefore, we use dynamic planning */# include <stdio. h ># include <algorithm> using namespace STD; # define min (A, B) (a) <(B )? (A) :( B) int d [2005] [1005], dat [2005]; int N, K; void dp () {int I, j; memset (D, 127, sizeof (d); for (I = 0; I <= N; I ++) d [I] [0] = 0; for (I = 2; I <= N; I ++) for (j = 1; j * 2 <= I; j ++) d [I] [J] = min (d [I-1] [J], d [I-2] [J-1] + dat [I-1]); // if the current group is selected (the square difference between I and I-1 exists dat [I-1]), // then dat [I-1] (the square difference between the current I and i-1) + d [I-2] [J-1] (pick a I-1 group in the J-1 to ensure that no heavy item will be picked)} int main () {int I; while (scanf ("% d", & N, & K )! =-1) {for (I = 1; I <= N; I ++) scanf ("% d", & dat [I]); sort (DAT + 1, DAT + n + 1); for (I = 1; I <n; I ++) DAT [I] = (DAT [I]-dat [I + 1]) * (DAT [I]-dat [I + 1]); DP (); printf ("% d \ n", d [N] [k]);} return 0 ;}