#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <map>
#include <functional>
#include <set>
using namespace std;
//
#define INF 0x7fffffff
int list[2001];
int dp[1001][2001];
int main(){
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
for(int i=1;i<=n;i++)
scanf("%d",&list[i]);
sort(list+1,list+1+n);
for(int i=1;i<=n;i++)
dp[0][i]=0;
for(int i=1;i<=k;i++){
for(int j=2*i;j<=n;j++){
if(j>2*i)
dp[i][j]=dp[i][j-1];
else
dp[i][j]=INF;
dp[i][j]=min(dp[i-1][j-2]+(list[j]-list[j-1])*(list[j]-list[j-1]),dp[i][j]);
}
}
printf("%d\n",dp[k][n]);
}
return 0;
}
/**************************************************************
Problem: 1452
User: cust123
Language: C++
Result: Accepted
Time:30 ms
Memory:9352 kb
****************************************************************/
题目1452:搬寝室
最新推荐文章于 2021-08-28 10:54:47 发布