# Division

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others)
Total Submission(s): 5053    Accepted Submission(s): 1980

Problem Description
Little D is really interested in the theorem of sets recently. There’s a problem that confused him a long time.
Let T be a set of integers. Let the MIN be the minimum integer in T and MAX be the maximum, then the cost of set T if defined as (MAX – MIN)^2. Now given an integer set S, we want to find out M subsets S1, S2, …, SM of S, such that

and the total cost of each subset is minimal.

Input
The input contains multiple test cases.
In the first line of the input there’s an integer T which is the number of test cases. Then the description of T test cases will be given.
For any test case, the first line contains two integers N (≤ 10,000) and M (≤ 5,000). N is the number of elements in S (may be duplicated). M is the number of subsets that we want to get. In the next line, there will be N integers giving set S.

Output
For each test case, output one line containing exactly one integer, the minimal total cost. Take a look at the sample output for format.

Sample Input
2
3 2
1 2 4
4 2
4 7 10 1

Sample Output
Case 1: 1
Case 2: 18
Hint
The answer will fit into a 32-bit signed integer.

Source

 1 #pragma comment(linker, "/STACK:102400000,102400000")
2 #include <cstdio>
3 #include <iostream>
4 #include <cstdlib>
5 #include <cstring>
6 #include <algorithm>
7 #include <cmath>
8 #include <cctype>
9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <bitset>
13 #include <string>
14 #include <complex>
15 #define ll __int64
16 #define mod 1000000007
17 using namespace std;
18 int t;
19 int n,m;
20 int a[10004];
21 int dp[5005][10004];
23 int main()
24 {
25     scanf("%d",&t);
26     for(int s=1; s<=t; s++)
27     {
28         scanf("%d %d",&n,&m);
29         for(int j=1; j<=n; j++)
30             scanf("%d",&a[j]);
31         sort(a+1,a+1+n);
32         for(int j=1; j<=n; j++)
33             dp[1][j]=(a[j]-a[1])*(a[j]-a[1]);
34         for(int i=2; i<=m; i++)
35         {
37             q[tail++]=i-1;
38             for(int j=i; j<=n; j++)
39             {
41                 {
43                     int x1=a[p1+1],x2=a[p2+1];
44                     int y1=dp[i-1][p1]+x1*x1,y2=dp[i-1][p2]+x2*x2;
45                     if(y2-y1<2*a[j]*(x2-x1))
47                     else
48                         break;
49                 }
51                 dp[i][j]=dp[i-1][k]+(a[j]-a[k+1])*(a[j]-a[k+1]);
53                 {
54                     int p1=q[tail-2],p2=q[tail-1],p3=j;
55                     int x1=a[p1+1],x2=a[p2+1],x3=a[p3+1];
56                     int y1=dp[i-1][p1]+x1*x1,y2=dp[i-1][p2]+x2*x2,y3=dp[i-1][p3]+x3*x3;
57                     if((y3-y2)*(x2-x1)<=(y2-y1)*(x3-x2))
58                         tail--;
59                     else
60                         break;
61                 }
62                 q[tail++]=j;
63             }
64         }
65         printf("Case %d: %d\n",s,dp[m][n]);
66     }
67     return 0;
68 }