#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ll long long
#define fi first
#define se second
#define ii pair<int,int>
#define fo(i,a,b) for (int i = (a); i <= (b); ++i)
#define fd(i,a,b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(), x.end()
template<typename T> void _do(T x) { cerr << x << '\n'; }
template<typename T, typename ...U> void _do(T x, U ...y) { cerr << x << ", "; _do(y...); }
#define db(...) cerr << #__VA_ARGS__ << " = "; _do(__VA_ARGS__);
bool ST;
const int N = 1e5+5, M = 1e7+5, inf = 1e9, LOG = 19;
struct EDGE {
int u, v, c;
bool operator < (const EDGE &o) const {
return c < o.c;
}
};
vector<EDGE> e;
int n, a[N], nxt[M], p[M];
bitset<M> b;
int find(int u) {
return (u == p[u] ? u : p[u] = find(p[u]));
}
bool merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return 0;
return p[u] = v, 1;
}
void solve() {
cin >> n;
fo(i,1,n) cin >> a[i], b[a[i]] = 1;
fo(i,1,M-1) p[i] = i;
int lst = -1;
fd(i,M-1,1) {
if (b[i]) lst = i;
nxt[i] = lst;
}
fo(i,1,M-1) if (b[i]) for (int j = i; j < M; j += i) {
if (nxt[j] == -1) break;
if (nxt[j] <= j + i) e.pb({i, nxt[j], nxt[j]%i});
}
sort(all(e));
ll ans = 0;
for (EDGE it : e) if (merge(it.u, it.v)) ans += it.c;
cout << ans;
}
bool EN;
int main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("A.inp", "r")) {
freopen("A.inp", "r", stdin);
// freopen("A.out", "w", stdout);
}
int testcase = 1;
// cin >> testcase;
while (testcase--) solve();
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen("A.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
79952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
79952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
79952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
90 ms |
95416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
44 ms |
83648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
165 ms |
132272 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
47 ms |
87320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
123 ms |
107824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
117 ms |
107844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
83912 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |