#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++)
{
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();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Incorrect |
450 ms |
50044 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
312 ms |
26000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
3932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
751 ms |
50644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
111 ms |
7116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
389 ms |
26164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
448 ms |
26020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
3864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |