using namespace std;
#include <bits/stdc++.h>
#define mapii map<int, int>
#define debug(a) cout << #a << ": " << a << endl
#define debuga1(a, l, r) fto(i, l, r) cout << a[i] << " "; cout << endl
#define fdto(i, r, l) for(int i = (r); i >= (l); --i)
#define fto(i, l, r) for(int i = (l); i <= (r); ++i)
#define ftoa(i, l, r, a) for(int i = (l); i <= (r); i += a)
#define forit(it, var) for(__typeof(var.begin()) it = var.begin(); it != var.end(); it++)
#define forrit(rit, var) for(__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); rit++)
#define ii pair<int, int>
#define iii pair<int, ii>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define ll long long
#define sizeX 10000005
#define next abcxyz
#define sz(a) (int)(a.size())
template <class T>
T min(T a, T b, T c) {
return min(a, min(b, c));
}
template <class T>
T max(T a, T b, T c) {
return max(a, max(b, c));
}
template <class T>
void read(vector<T> &v) {
T x;
cin >> x;
v.pb(x);
}
struct edge {
int u, v;
edge() {}
edge(int u, int v): u(u), v(v) {}
};
int n, next[sizeX];
bool avail[sizeX];
vector<int> a;
vector<edge> e[10000001];
class UFDS {
private: int n; vector<int> pset;
public:
UFDS(int n) {
pset.resize(n);
fto(i, 0, n-1) pset[i] = i;
}
int findSet(int u) {
return (pset[u] == u) ? u : pset[u] = findSet(pset[u]);
}
bool unionSet(int u, int v) {
int p = findSet(u), q = findSet(v);
if (p == q) return false;
pset[p] = q;
return true;
}
};
const int BUFFSIZE = 4096;
inline char getChar() {
static char buff[BUFFSIZE];
static int pos = BUFFSIZE;
if (pos == BUFFSIZE) {
fread(buff, 1, BUFFSIZE, stdin);
pos = 0;
}
return buff[pos++];
}
inline int readInt() {
int q = 0;
char c;
do {
c = getChar();
} while (!isdigit(c));
do {
q = (q << 1) + (q << 3) + (c - '0');
c = getChar();
} while (isdigit(c));
return q;
}
int main () {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
n = readInt();
int maxX = 0;
fto(i, 0, n-1) {
int x = readInt();
avail[x] = true;
maxX = max(maxX, x);
}
next[maxX+1] = -1;
fdto(x, maxX, 1) {
if (avail[x]) {
next[x] = sz(a);
a.pb(x);
} else next[x] = next[x+1];
}
memset(avail, false, sizeof avail);
fto(i, 0, sz(a)-1) {
vector<int> save;
ftoa(k, a[i], maxX, a[i]) {
int j = next[k + (k == a[i])];
if (j >= 0 && !avail[j]) {
e[a[j] % a[i]].pb(edge(i, j));
avail[j] = true; save.pb(j);
}
}
forit(it, save) avail[*it] = false;
}
UFDS s(a.size());
int ans = 0;
for (int i = 0; i <= 10000000; i++) {
forit(it, e[i]) {
if (s.unionSet(it->u, it->v)) ans += i;
}
}
printf("%d", ans);
return 0;
}
dXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbWFwaWkgbWFwPGludCwgaW50PgojZGVmaW5lIGRlYnVnKGEpIGNvdXQgPDwgI2EgPDwgIjogIiA8PCBhIDw8IGVuZGwKI2RlZmluZSBkZWJ1Z2ExKGEsIGwsIHIpIGZ0byhpLCBsLCByKSBjb3V0IDw8IGFbaV0gPDwgIiAiOyBjb3V0IDw8IGVuZGwKI2RlZmluZSBmZHRvKGksIHIsIGwpIGZvcihpbnQgaSA9IChyKTsgaSA+PSAobCk7IC0taSkKI2RlZmluZSBmdG8oaSwgbCwgcikgZm9yKGludCBpID0gKGwpOyBpIDw9IChyKTsgKytpKQojZGVmaW5lIGZ0b2EoaSwgbCwgciwgYSkgZm9yKGludCBpID0gKGwpOyBpIDw9IChyKTsgaSArPSBhKQojZGVmaW5lIGZvcml0KGl0LCB2YXIpIGZvcihfX3R5cGVvZih2YXIuYmVnaW4oKSkgaXQgPSB2YXIuYmVnaW4oKTsgaXQgIT0gdmFyLmVuZCgpOyBpdCsrKQojZGVmaW5lIGZvcnJpdChyaXQsIHZhcikgZm9yKF9fdHlwZW9mKHZhci5yYmVnaW4oKSkg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ICBpbnQgbWF4WCA9IDA7CiAgICBmdG8oaSwgMCwgbi0xKSB7CiAgICAgICAgaW50IHggPSByZWFkSW50KCk7CiAgICAgICAgYXZhaWxbeF0gPSB0cnVlOwogICAgICAgIG1heFggPSBtYXgobWF4WCwgeCk7CiAgICB9CiAKICAgIG5leHRbbWF4WCsxXSA9IC0xOwogICAgZmR0byh4LCBtYXhYLCAxKSB7CiAgICAgICAgaWYgKGF2YWlsW3hdKSB7CiAgICAgICAgICAgIG5leHRbeF0gPSBzeihhKTsKICAgICAgICAgICAgYS5wYih4KTsKICAgICAgICB9IGVsc2UgbmV4dFt4XSA9IG5leHRbeCsxXTsKICAgIH0KIAogICAgbWVtc2V0KGF2YWlsLCBmYWxzZSwgc2l6ZW9mIGF2YWlsKTsKICAgIGZ0byhpLCAwLCBzeihhKS0xKSB7CiAgICAgICAgdmVjdG9yPGludD4gc2F2ZTsKICAgICAgICBmdG9hKGssIGFbaV0sIG1heFgsIGFbaV0pIHsKICAgICAgICAgICAgaW50IGogPSBuZXh0W2sgKyAoayA9PSBhW2ldKV07CiAgICAgICAgICAgIGlmIChqID49IDAgJiYgIWF2YWlsW2pdKSB7CiAgICAgICAgICAgICAgICBlW2Fbal0gJSBhW2ldXS5wYihlZGdlKGksIGopKTsKICAgICAgICAgICAgICAgIGF2YWlsW2pdID0gdHJ1ZTsgc2F2ZS5wYihqKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmb3JpdChpdCwgc2F2ZSkgYXZhaWxbKml0XSA9IGZhbHNlOwogICAgfQogCiAgICBVRkRTIHMoYS5zaXplKCkpOwogICAgaW50IGFucyA9IDA7CiAgICBmb3IgKGludCBpID0gMDsgaSA8PSAxMDAwMDAwMDsgaSsrKSB7CgkgICAgZm9yaXQoaXQsIGVbaV0pIHsKCSAgICAgICAgaWYgKHMudW5pb25TZXQoaXQtPnUsIGl0LT52KSkgYW5zICs9IGk7CgkgICAgfQogICAgfQogCiAgICBwcmludGYoIiVkIiwgYW5zKTsKIAogICAgcmV0dXJuIDA7Cn0KIAog