fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define TIME (1.0*clock()/CLOCKS_PER_SEC)
  4. using namespace std;
  5. const int N = 2e5+1;
  6. const ll INF =1e18;
  7. const int Q =1e9+7;
  8. ll a[N];
  9. ll n,k;
  10. namespace sub1{
  11. int main(){
  12. sort(a+1,a+n+1);
  13. vector<vector<ll>>dp(n+1,vector<ll>(k+1,INF));
  14. dp[0][0] = 0;
  15. for(ll i=1;i<=n;++i){
  16. dp[i][0] = 0;
  17. for(ll j=0;j<=min(k,i);++j){
  18. dp[i][j] = min(dp[i][j],dp[i-1][j]);
  19. if(i >= 2 && j >= 1) dp[i][j] = min(dp[i][j],dp[i-2][j-1]+(a[i]-a[i-1])*(a[i]-a[i-1]));
  20. }
  21. }
  22. cout << dp[n][k] << endl;
  23. return 0;
  24. }
  25. }
  26. int main(){
  27. cin.tie(0)->sync_with_stdio(0);
  28. cout.tie(0);
  29. cin >> n >> k;
  30. for(ll i=1;i<=n;++i) cin >> a[i];
  31. sub1::main();
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
0