#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) {
nxt[i] = lst;
if (b[i]) lst = i;
}
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});
if (j + i < M && b[i+j]) e.pb({i, j+i, 0});
}
sort(all(e));
// for (EDGE it : e) dbg(it.u, it.v, it.c);
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();
// cerr << '\n' << abs(&EN - &ST) / 1024.0 / 1024.0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen("A.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
sirni.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | freopen("A.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
79952 KB |
Output is correct |
2 |
Correct |
59 ms |
83400 KB |
Output is correct |
3 |
Correct |
30 ms |
80200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
79748 KB |
Output is correct |
2 |
Correct |
188 ms |
81296 KB |
Output is correct |
3 |
Correct |
35 ms |
80208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
79952 KB |
Output is correct |
2 |
Correct |
27 ms |
79696 KB |
Output is correct |
3 |
Correct |
28 ms |
79944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
107524 KB |
Output is correct |
2 |
Correct |
309 ms |
181140 KB |
Output is correct |
3 |
Correct |
125 ms |
107700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
83656 KB |
Output is correct |
2 |
Correct |
188 ms |
132236 KB |
Output is correct |
3 |
Correct |
100 ms |
107444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
131508 KB |
Output is correct |
2 |
Correct |
366 ms |
179352 KB |
Output is correct |
3 |
Correct |
111 ms |
107700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
87236 KB |
Output is correct |
2 |
Correct |
369 ms |
179488 KB |
Output is correct |
3 |
Correct |
115 ms |
107700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
106932 KB |
Output is correct |
2 |
Correct |
1812 ms |
474820 KB |
Output is correct |
3 |
Correct |
155 ms |
107928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
107904 KB |
Output is correct |
2 |
Correct |
2281 ms |
474964 KB |
Output is correct |
3 |
Correct |
159 ms |
107920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
83912 KB |
Output is correct |
2 |
Correct |
2586 ms |
474920 KB |
Output is correct |
3 |
Correct |
127 ms |
107696 KB |
Output is correct |