#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define f first
#define s second
#define mkp make_pair
#define pii pair<int, int>
struct DSU {
vector<int> e;
DSU(int N) { e = vector<int>(N, -1); }
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y];
e[y] = x;
return true;
}
};
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<int> v(n);
for(int i = 0; i < n; i++){
cin >> v[i];
}
sort(all(v));
v.erase(unique(all(v)), v.end());
n = sz(v);
vector<int> nxt(v.back() + 1, -1);
for(int i = 0; i < n; i++){
nxt[v[i]] = i;
}
for(int i = v.back() - 1; i >= 0; i--){
if(nxt[i] == -1){
nxt[i] = nxt[i + 1];
}
}
vector<vector<pii>> links(v.back() + 1);
for(int i = 0; i < n - 1; i++){
links[v[i + 1] % v[i]].pb(mkp(i, i + 1));
for(int j = 2 * v[i]; j <= v.back(); j += v[i]){
links[v[nxt[j]] % v[i]].pb(mkp(nxt[j], i));
}
}
DSU dsu(n);
int ans = 0;
for(int i = 0; i <= v.back(); i++){
for(auto x : links[i]){
if(dsu.unite(x.f, x.s)){
ans += i;
}
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
313428 KB |
Output is correct |
2 |
Correct |
136 ms |
368724 KB |
Output is correct |
3 |
Correct |
69 ms |
312728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Runtime error |
480 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
313428 KB |
Output is correct |
2 |
Correct |
69 ms |
312916 KB |
Output is correct |
3 |
Correct |
61 ms |
313684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
52840 KB |
Output is correct |
2 |
Correct |
120 ms |
113552 KB |
Output is correct |
3 |
Correct |
67 ms |
77620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
35676 KB |
Output is correct |
2 |
Correct |
80 ms |
76964 KB |
Output is correct |
3 |
Correct |
42 ms |
37904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
80440 KB |
Output is correct |
2 |
Correct |
152 ms |
152596 KB |
Output is correct |
3 |
Correct |
66 ms |
75164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
13972 KB |
Output is correct |
2 |
Correct |
154 ms |
147856 KB |
Output is correct |
3 |
Correct |
64 ms |
75576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
339464 KB |
Output is correct |
2 |
Runtime error |
514 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
349248 KB |
Output is correct |
2 |
Runtime error |
453 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
318292 KB |
Output is correct |
2 |
Runtime error |
735 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |