#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAXN 100005
#define MAXC 10000005
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
#define f first
#define s second
ll n, c[MAXN], p[MAXN], sz[MAXN], r = 0;
priority_queue<pair<ll, pair<ll, ll>>> pq;
ll find(ll x) { return (p[x] == x) ? x : (p[x] = find(p[x])); }
bool onion(ll a, ll b){
a = find(a), b = find(b);
if(a == b) return false;
if(sz[a] < sz[b]) swap(a, b);
p[a] = p[b] = a;
sz[a] += sz[b];
return true;
}
int main(){
cin >> n;
FOR(i, 1, n) {
cin >> c[i];
p[i] = i, sz[i] = 1;
}
sort(c + 1, c + 1 + n);
ll n2 = 1;
FOR(i, 1, n) c[n2] = c[i], n2 += (i != n && c[i + 1] != c[i]);
n = n2;
FOR(i, 1, n){
ll j = i + 1;
for(ll mn = c[i]; mn < MAXC;) {
ll lo = j, hi = n + 1;
while(lo < hi){
ll md = (lo + hi) / 2;
if(mn <= c[md] || md == n + 1){
hi = md;
}else{
lo = md + 1;
}
}
if(j == n + 1) break;
pq.push({-(c[lo] % c[i]), {i, lo}}), j = lo + 1;
mn = ((c[lo] / c[i]) + 1) * c[i];
}
}
while(pq.size()){
auto pqi = pq.top(); pq.pop();
r -= pqi.f * onion(pqi.s.f, pqi.s.s);
}
cout << r << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |