#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <utility>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
#define pb push_back
#define all(x) begin(x), end(x)
#define space " "
#define TEST_CASES int a; cin >> a; for (int i = 0; i < a; i++) {solve(); cout << endl;}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
struct edge {
int x, y, c;
};
bool cmp(const edge &x, const edge &y) {
if (x.c != y.c) return x.c < y.c;
if (x.x != y.x) return x.x < y.x;
return x.y < y.y;
}
class DSU{
vector<int> pred;
vector<int> sizes;
public:
DSU(int size) : pred(size), sizes(size, 1){
for (int i = 0; i < size; i++){
pred[i] = i;
}
}
int size(int x){
return (pred[x] == x ? sizes[x] : sizes[x] = sizes[find(x)]);
}
int find(int x){
return (pred[x] == x ? x : pred[x] = find(pred[x]));
}
bool unite(int x, int y){
int xp = find(x);
int yp = find(y);
if (xp == yp){
return false;
}
if (sizes[xp] < sizes[yp]){
swap(xp, yp);
}
sizes[xp] += sizes[yp];
pred[yp] = xp;
return true;
}
};
void solve() {
int n; cin >> n;
vector<int> vec(n);
for (int i = 0; i < n; i++) cin >> vec[i];
sort(all(vec));
vec.erase(unique(all(vec)), vec.end());
n = vec.size();
vector<vector<int>> can(1e7 + 1);
for (int i = 0; i < n; i++) {
for (int j = vec[i]; j <= 1e7; j += vec[i]) {
can[j].pb(i);
}
}
vector<int> cans;
for (int i = 1; i <= 1e7; i++) {
if (!can[i].empty()) {
cans.pb(i);
}
}
vector<edge> edges;
for (int i = 0; i < n; i++) {
if (can[vec[i]].size() > 1) {
for (auto x : can[vec[i]]) {
if (x == i) continue;
edges.pb({x, i, 0});
}
}
else {
auto it = lower_bound(all(cans), vec[i]);
if (it != cans.begin()) {
--it;
for (auto x : can[*it]) {
edges.pb({x, i, vec[i] - *it});
}
}
}
}
sort(all(edges), cmp);
DSU dsu(n);
ll res = 0;
for (int i = 0; i < edges.size(); i++) {
//cout << edges[i].x << " " << edges[i].y << " " << edges[i].c << endl;
if (dsu.unite(edges[i].x, edges[i].y)) {
res += edges[i].c;
}
}
cout << res;
}
int main() {
FAST_IO;
//freopen("fencedin.in", "r", stdin);
//freopen("fencedin.out", "w", stdout);
//TEST_CASES;
solve(); cout << endl;
/*int a; cin >> a;
for (int i = 1; i <= a; i++){
cout << "Case #" << i << ": ";
solve();
cout << endl;
}*/
}