src2020/ll1882_chopsticks/main.cpp

34 lines
879 B
C++

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int kuaiz,ke,ren=3,a[101];
cin>>kuaiz>>ke;
for(int i=1;i<=kuaiz;i++){
cin>>a[i];;
}
ren+=ke;
if(ren*2>kuaiz){
cout<<"-1";
return 0;
}
sort(a+1,a+kuaiz+1);
int f[101][51]={0};
bool pan[101][51]={0};
for(int i=2;i<=kuaiz;i++){
for(int j=1;j<=ren;j++){
if(f[i-1][j]==0&&pan[i-1][j]==0){
f[i][j]=f[i-2][j-1]+(a[i]-a[i-1])*(a[i]-a[i-1]);
pan[i][j]=1;
}else{
f[i][j]= min (f[i-1][j],f[i-2][j-1]+(a[i]-a[i-1])*(a[i]-a[i-1]));
pan[i][j]=1;
}
//cout<<i<<" "<<j<<" "<<f[i][j]<<endl;
if(j==i/2){
break;
}
}
}
cout<<f[kuaiz][ren];
}