#include #include #include using namespace std; const long long INF = LLONG_MAX / 2; int main() { int n, k; cin >> n >> k; long long A[n]; for(int i = 0; i < n; i++) cin >> A[i]; sort(A, A + n); // DP[i][j] represents the minimum sum of sizes of sets obtained by dividing the first i elements into j subsets. long long DP[n + 1][k + 1]; for(int i = 0; i <= n; i++) { for(int j = 0; j <= k; j++) { DP[i][j] = INF; } } // Base case: If we have 0 elements, the minimum sum is 0 for any number of subsets. for(int j = 0; j <= k; j++) { DP[0][j] = 0; } for(int i = 1; i <= n; i++) { for(int j = 1; j <= k; j++) { for(int p = 0; p < i; p++) { // Calculate the size of the current subset. long long size = A[i - 1] - A[p]; // Update the DP table. DP[i][j] = min(DP[i][j], DP[p][j - 1] + size); } } } // The answer will be stored in DP[n][k]. cout << DP[n][k] << endl; return 0; }