#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;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZW5kbCAnXG4nCgovLyNwcmFnbWEgR0NDIG9wdGltaXplICgiTzMiKQovLyNwcmFnbWEgR0NDIHRhcmdldCAoInNzZTQiKQoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdGVtcGxhdGU8Y2xhc3MgVCwgY2xhc3MgVDI+IGlubGluZSBpbnQgY2hrbWF4KFQgJngsIGNvbnN0IFQyICZ5KSB7IHJldHVybiB4IDwgeSA/IHggPSB5LCAxIDogMDsgfQp0ZW1wbGF0ZTxjbGFzcyBULCBjbGFzcyBUMj4gaW5saW5lIGludCBjaGttaW4oVCAmeCwgY29uc3QgVDIgJnkpIHsgcmV0dXJuIHggPiB5ID8geCA9IHksIDEgOiAwOyB9CmNvbnN0IGludCBNQVhOID0gKDEgPDwgMjApOwoKaW50IG4sIG07CmludCBhW01BWE5dOwoKdm9pZCByZWFkKCkKewoJY2luID4+IG4gPj4gbTsKCWZvcihpbnQgaSA9IDA7IGkgPCBtOyBpKyspCgkJY2luID4+IGFbaV07Cn0KCnZvaWQgc29sd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