#include <bits/stdc++.h>
using namespace std;
class DSU {
vector<int> parent, sz;
public:
DSU(int n) : parent(n), sz(n, 1) {
for (int i = 0; i < n; ++i) parent[i] = i;
}
int find(int x) {
return parent[x] == x ? x : parent[x] = find(parent[x]);
}
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
if (sz[x] < sz[y]) swap(x, y);
parent[y] = x;
sz[x] += sz[y];
return true;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
int max_val = a.back();
vector<int> nxt(max_val + 1, -1);
for (int i = 0; i < (int)a.size(); ++i) nxt[a[i]] = i;
for (int v = max_val - 1; v >= 0; --v)
if (nxt[v] == -1) nxt[v] = nxt[v + 1];
vector<vector<pair<int, int>>> edges_by_cost(max_val + 1);
for (int i = 0; i < (int)a.size() - 1; ++i) {
edges_by_cost[a[i + 1] % a[i]].emplace_back(i, i + 1);
for (int mult = 2 * a[i]; mult <= max_val; mult += a[i]) {
int j = nxt[mult];
edges_by_cost[a[j] % a[i]].emplace_back(i, j);
}
}
long long total_cost = 0;
DSU dsu(a.size());
for (int cost = 0; cost <= max_val; ++cost) {
for (const auto &e : edges_by_cost[cost]) {
if (dsu.unite(e.first, e.second)) {
total_cost += cost;
}
}
}
cout << total_cost << '\n';
return 0;
}