#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#define ll long long int
#define oo 1e9
#define pii pair<ll, int>
const int MAX = 1e5 + 5;
int n;
vector<array<int, 3>> edges;
int par[MAX];
int findSet(int u){
if(par[u] < 0) return u;
return par[u] = findSet(par[u]);
}
bool unite(int u, int v){
u = findSet(u);
v = findSet(v);
if(u == v) return false;
if(-par[u] < -par[v]) swap(u, v);
par[u] += par[v];
par[v] = u;
return true;
}
bool check(int u, int v){
u = findSet(u);
v = findSet(v);
if(u == v) return true;
return false;
}
void solve(){
memset(par, -1, sizeof(par));
cin >> n;
vector<int> v;
for (int i = 0; i < n; i++)
{
int a; cin >> a;
v.push_back(a);
}
sort(v.begin(), v.end());
int m = v[v.size() - 1];
for (int i = 0; i < n; i++)
{
if(v[i] == v[i - 1]) continue;
for (int r = v[i]; r <= m; r += v[i])
{
auto itr = lower_bound(v.begin(), v.end(), r);
if(r == v[i]){
itr = upper_bound(v.begin(), v.end(), r);
}
if(itr == v.end()){
continue;
}
edges.push_back({(*itr) % v[i], i, int(itr - v.begin())});
}
}
sort(edges.begin(), edges.end());
int ans = 0;
for(array<int, 3> a : edges){
if(unite(a[1], a[2])){
ans += a[0];
}
}
cout << ans;
}
int main(){
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
354 ms |
50048 KB |
Output is correct |
3 |
Correct |
5 ms |
1172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
852 KB |
Output is correct |
2 |
Runtime error |
994 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
329 ms |
26292 KB |
Output is correct |
2 |
Correct |
1121 ms |
51268 KB |
Output is correct |
3 |
Correct |
562 ms |
51328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
4108 KB |
Output is correct |
2 |
Correct |
551 ms |
51248 KB |
Output is correct |
3 |
Correct |
330 ms |
26628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
692 ms |
50956 KB |
Output is correct |
2 |
Correct |
1559 ms |
100560 KB |
Output is correct |
3 |
Correct |
472 ms |
26776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
7352 KB |
Output is correct |
2 |
Correct |
1527 ms |
100496 KB |
Output is correct |
3 |
Correct |
473 ms |
26760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
410 ms |
26336 KB |
Output is correct |
2 |
Runtime error |
2078 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
477 ms |
26316 KB |
Output is correct |
2 |
Runtime error |
1918 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
4040 KB |
Output is correct |
2 |
Runtime error |
2503 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |