#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 1e5 + 227;
const int mxM = 1e7 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
struct DSU{
vector<int> lab;
DSU(){}
DSU(int n)
{
lab.resize(n + 7 , -1);
}
int root(int u) {return lab[u] < 0 ? u : lab[u] = root(lab[u]);}
bool union_root(int u , int v)
{
u = root(u);
v = root(v);
if(u == v) return false;
if(lab[u] > lab[v]) swap(u , v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
} dsu;
struct item{
int u , v , c;
bool operator<(const item &a) {
return c < a.c;
}
};
int a[mxN];
int best[mxM];
int MAX = 1e7;
vector<item> edges;
void solve()
{
memset(best , 0x3f , sizeof(best));
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++) {
int x;
cin >> x;
best[x] = x;
}
dsu = DSU(MAX);
for(int i = MAX ; i >= 1 ; i--) minimize(best[i] , best[i + 1]);
for(int i = 1 ; i <= MAX ; i++) {
if(best[i] != i) continue;
for(int j = i ; j <= MAX ; j += i) {
if(best[j] == best[0]) break;
edges.pb({i , best[j] , best[j] % i});
}
}
sort(edges.begin() , edges.end());
ll res = 0;
for(auto it : edges) {
if(dsu.union_root(it.u , it.v) == true) {
res += it.c;
}
}
cout << res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen(TASK".inp" , "r" , stdin);
//freopen(TASK".out" , "w" , stdout);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
78720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
44 ms |
78744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
78748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
131 ms |
103260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
81772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
217 ms |
127984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
74 ms |
84804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
211 ms |
103308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
223 ms |
103296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
82 ms |
81720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |