#ifdef LOCAL
#pragma optimize("", off)
#include "localstd1.h"
#else
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/allocator.h>
#pragma GCC target("avx2,bmi,bmi2")
#include <bits/stdc++.h>
#endif

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

#define all(a) (a).begin(), (a).end()

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	ll n, m; cin >> n >> m;
	vector<ll> a(m);
	map<ll, ll> cnt;
	for (int i = 0; i < m; i++) {
		cin >> a[i];
		cnt[a[i]]++;
	}
	if (cnt.size() == 1) {
		auto [val, k] = *cnt.begin();
		if (n >= m) cout << a[0] * n;
		else if ((m - n) % 2 == 0) cout << a[0] * n;
		else cout << a[0] * n - a[0];
	}
	else {
		int cntp = n, cntm = 0;
		auto b = a;
		sort(all(a));
		a.erase(unique(all(a)), a.end());
		ll mn = a[0], mx = a[1];
		ll cntmn = cnt[mn], cntmx = cnt[mx];
		ll res = 0;
		for (ll x : b) {
			if (x == mn) {
				if (cntm && cntmx >= cntp) {
					cntm--;
					res -= mn;
					cntp++;
				}
				else {
					cntp--;
					cntm++;
					res += mn;
				}
				cntmn--;
			}
			else {
				if (cntp) {
					res += mx;
					cntp--;
					cntm++;
				}
				else {
					res -= mx;
					cntp++;
					cntm--;
				}
				cntmx--;
			}
		}
		cout << res;
	}
	
	return 0;
}