注意:dp[i][j]初始化的时候不能小于0xffffff明显大于2^15但是吧再小就WA了
#include<iostream> #include<algorithm> #include<cstring> using namespace std; int dp[2002][1002] = {0}; int main() { int n,k; while(cin>>n>>k){ int i,j; int w[2002] = {0}; for(i=0;i<n;++i)cin>>w[i]; sort(w,w+n); for(i=0;i<=n;i++) for(j=1;j<=k;j++) dp[i][j]=0xfffffff;//最少6个F for(i=2;i<=n;i++) for(j=1;j<=i/2&&j<=k;j++) dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(w[i-1]-w[i-2])*(w[i-1]-w[i-2])); cout<<dp[n][k]<<endl; } return 0; }