#include <bits/stdc++.h>
#define endl '\n'

//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")

using namespace std;
template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);

int n, m;
int a[MAXN];

void read()
{
	cin >> n >> m;
	for(int i = 0; i < m; i++)
		cin >> a[i];
}

void solve()
{
	sort(a, a + m);

	priority_queue<pair<int64_t, int> > pq;
	for(int i = 0; i < m; i++)
		pq.push({-a[i], i});

	int64_t sum = 0;
	for(int i = 0; i < n; i++)
	{
		auto it = pq.top();
		sum -= it.first;
		pq.pop();

		for(int j = it.second; j < m; j++)
			pq.push({it.first - a[j], j});
	}

	cout << sum << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	read();
	solve();
	return 0;
}

