#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ii pair<int, int>
int pa[100000], sz[100000];
int findSet(int x) {
return pa[x] == x ? x : pa[x] = findSet(pa[x]);
}
void unite(int a, int b) {
a = findSet(a); b = findSet(b);
if (sz[a] > sz[b]) swap(a, b);
pa[a] = b;
sz[b] += sz[a];
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(false);
int n; cin >> n;
set<int> nums;
for (int i = 0; i < n; i++) {
int p; cin >> p;
nums.insert(p);
}
map<int, int> idx;
int ctr = 0;
for (int num : nums) {
idx[num] = ctr++;
}
for (int i = 0; i < n; i++) {
sz[i] = 1;
pa[i] = i;
}
vector<pair<int, ii>> edges;
for (int num : nums) {
/*
for (int i = num; i <= 1e7; i += num) {
auto it = nums.upper_bound(i);
if (it != nums.end()) {
edges.pb(mp((*it) % num, mp(idx[num], idx[*it])));
}
}
*/
for (int num2 : nums) {
edges.pb(mp(min(num%num2, num2%num), mp(idx[num], idx[num2])));
}
}
sort(edges.begin(), edges.end());
ll ans = 0;
for (auto e : edges) {
if (e.s.s >= ctr) continue;
if (findSet(e.s.f) != findSet(e.s.s)) {
ans += e.f;
unite(e.s.f, e.s.s);
}
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
12888 KB |
Output is correct |
2 |
Correct |
172 ms |
12888 KB |
Output is correct |
3 |
Correct |
199 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
12892 KB |
Output is correct |
2 |
Correct |
112 ms |
6764 KB |
Output is correct |
3 |
Correct |
202 ms |
12896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
12920 KB |
Output is correct |
2 |
Correct |
159 ms |
12892 KB |
Output is correct |
3 |
Correct |
206 ms |
12884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5096 ms |
404520 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4547 ms |
786436 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5084 ms |
404316 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5097 ms |
487236 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5112 ms |
404864 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5063 ms |
405008 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4793 ms |
786436 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |