#include #include #include #include #include std::vector dist; int ddist[1001][1001]; const long long INF = LLONG_MAX; long long min_cost = INF; void isCollect(long long cost, int n, int now, std::vector visited){ if(cost < 0) return; if(n == 0){ min_cost = std::min(min_cost, cost); return; } for(size_t i = 0; i < visited.size(); i++){ if(visited[i] == false){ visited[i] = true; isCollect(cost + ddist[now][i], n - 1, i, visited); visited[i] = false; } } } int main(){ int n, m; std::cin >> n >> m; dist.resize(m); for(int i = 0; i < m; i++){ std::cin >> dist[i]; } dist.push_back(0); std::sort(dist.begin(), dist.end()); for(size_t i = 0; i < dist.size(); i++){ for(size_t j = 0; j < dist.size(); j++){ ddist[i][j] = std::abs(dist[i] - dist[j]); } } std::vector visited(dist.size(), false); int start = 0; for(size_t i = 0; i < dist.size(); i++){ if(dist[i] == 0){ start = i; visited[i] = true; break; } } isCollect(0, n, start, visited); std::cout << min_cost << std::endl; return 0; }