#include #include #include using namespace std; int N, K; vector nums; long dp[1000][1000]; long choose(int now, int array_now){ if(now == N){ return 0; }else if(dp[now][array_now] != -1){ return dp[now][array_now]; }else if(array_now == K){ return dp[now][array_now] = choose(now + 1, array_now) + nums[now] - nums[now - 1]; }else{ return dp[now][array_now] = min(choose(now + 1, array_now) + nums[now] - nums[now - 1], choose(now + 1, array_now + 1)); } } int main(){ cin >> N >> K; nums.resize(N); for(int i = 0; i < N; i++){ cin >> nums[i]; } sort(nums.begin(), nums.end()); for(int i = 0; i < 1000; i++){ for(int j = 0; j < 1000; j++){ dp[i][j] = -1; } } cout << choose(1, 1) << endl; }