This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 1e7 + 5;
int a[N];
int p[N];
int val[N];
int n;
bool mini(int &a, int b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
n = unique(a + 1, a + n + 1) - (a + 1);
for (int i = 1; i <= n; i++) {
p[a[i]] = a[i];
val[a[i]] = 1e9;
}
int mx = a[n];
for (int i = mx - 1; i >= 1; i--)
if (!p[i])
p[i] = p[i + 1];
val[a[1]] = 0;
priority_queue <pair <int, int>> heap;
heap.push(mp(0, a[1]));
long long ans = 0;
while (heap.size()) {
int i = heap.top().se;
int cur = -heap.top().fi;
heap.pop();
// cout << i << " " << cur << endl;
if (val[i] != cur)
continue;
ans += cur;
val[i] = -1e9;
for (int j = i; j <= mx; j += i)
if (mini(val[p[j]], p[j] - j))
heap.push(mp(-val[p[j]], p[j]));
}
cout << ans;
return 0;
}
# | 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... |