#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair < int, int > ii;
const int N = 1e5 + 5;
const int M = 1e7 + 5;
int n;
int a[N], root[N], cnt[M], nxt[M];
vector < ii > v;
int f(int x) {
if(x == root[x])
return x;
return root[x] = f(root[x]);
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", a + i), root[i] = i;
sort(a + 1, a + n + 1);
n = unique(a + 1, a + n + 1) - a - 1;
for(int i = 1; i <= n; i++)
nxt[a[i]] = i;
nxt[M - 1] = n + 1;
for(int i = M - 2; i >= 1; i--)
if(!nxt[i])
nxt[i] = nxt[i + 1];
ll ans = 0;
for(int i = n - 1; i >= 1; i--) {
for(int j = a[i]; j < M; j += a[i]) {
int k = nxt[j + (a[i] == j)];
if(k > n)
break;
if(a[k] - j < a[i]) {
cnt[a[k] - j]++;
v.push_back({i, k});
}
else
j = a[k] / a[i] * a[i] - a[i];
}
}
for(int i = 1; i < M; i++)
cnt[i] += cnt[i - 1];
auto res = v;
for(auto x : v)
res[--cnt[a[x.second] % a[x.first]]] = x;
for(auto x : res) {
if(f(x.first) == f(x.second))
continue;
ans += a[x.second] % a[x.first];
root[f(x.first)] = f(x.second);
}
printf("%lld\n", ans);
return 0;
}
I2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdHlwZWRlZiBsb25nIGxvbmcgbGw7CnR5cGVkZWYgcGFpciA8IGludCwgaW50ID4gaWk7Cgpjb25zdCBpbnQgTiA9IDFlNSArIDU7CmNvbnN0IGludCBNID0gMWU3ICsgNTsKCmludCBuOwppbnQgYVtOXSwgcm9vdFtOXSwgY250W01dLCBueHRbTV07CnZlY3RvciA8IGlpID4gdjsKCmludCBmKGludCB4KSB7CiAgICBpZih4ID09IHJvb3RbeF0pCiAgICAgICAgcmV0dXJuIHg7CiAgICByZXR1cm4gcm9vdFt4XSA9IGYocm9vdFt4XSk7Cn0KCmludCBtYWluKCkgewogICAgCiAgICBzY2FuZigiJWQiLCAmbik7CiAgICAKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKQogICAgICAgIHNjYW5mKCIlZCIsIGEgKyBpKSwgcm9vdFtpXSA9IGk7CiAgICAKICAgIHNvcnQoYSArIDEsIGEgKyBuICsgMSk7CiAgICBuID0gdW5pcXVlKGEgKyAxLCBhICsgbiArIDEpIC0gYSAtIDE7CiAgICAKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKQogICAgICAgIG54dFthW2ldXSA9IGk7CiAgICAKICAgIG54dFtNIC0gMV0gPSBuICsgMTsKICAgIGZvcihpbnQgaSA9IE0gLSAyOyBpID49IDE7IGktLSkKICAgICAgICBpZighbnh0W2ldKQogICAgICAgICAgICBueHRbaV0gPSBueHRbaSArIDFdOwogICAgCiAgICBsbCBhbnMgPSAwOwogICAgCiAgICBmb3IoaW50IGkgPSBuIC0gMTsgaSA+PSAxOyBpLS0pIHsKICAgICAgICBmb3IoaW50IGogPSBhW2ldOyBqIDwgTTsgaiArPSBhW2ldKSB7CiAgICAgICAgICAgIGludCBrID0gbnh0W2ogKyAoYVtpXSA9PSBqKV07CiAgICAgICAgICAgIGlmKGsgPiBuKQogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGlmKGFba10gLSBqIDwgYVtpXSkgewogICAgICAgICAgICAgICAgY250W2Fba10gLSBqXSsrOwogICAgICAgICAgICAgICAgdi5wdXNoX2JhY2soe2ksIGt9KTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICBqID0gYVtrXSAvIGFbaV0gKiBhW2ldIC0gYVtpXTsKICAgICAgICB9CiAgICB9CiAgICAKICAgIGZvcihpbnQgaSA9IDE7IGkgPCBNOyBpKyspCiAgICAgICAgY250W2ldICs9IGNudFtpIC0gMV07CiAgICAKICAgIGF1dG8gcmVzID0gdjsKICAgIAogICAgZm9yKGF1dG8geCA6IHYpCiAgICAgICAgcmVzWy0tY250W2FbeC5zZWNvbmRdICUgYVt4LmZpcnN0XV1dID0geDsKCiAgICBmb3IoYXV0byB4IDogcmVzKSB7CiAgICAgICAgaWYoZih4LmZpcnN0KSA9PSBmKHguc2Vjb25kKSkKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgYW5zICs9IGFbeC5zZWNvbmRdICUgYVt4LmZpcnN0XTsKICAgICAgICByb290W2YoeC5maXJzdCldID0gZih4LnNlY29uZCk7CiAgICB9CiAgICAKICAgIHByaW50ZigiJWxsZFxuIiwgYW5zKTsKICAgIAogICAgcmV0dXJuIDA7Cn0KCgo=