#include <bits/stdc++.h>
using namespace std;
#define N 100005
#define se second
#define fi first
int n, par[N];
long long res = 0;
vector<int> p;
struct ii
{
int u, v, w;
bool operator < (ii o){
return w < o.w;
}
};
int find(int u){
if (u == par[u]) return u;
return par[u] = find(par[u]);
}
void unin(int u, int v, int w){
u = find(u);
v = find(v);
if (u == v) return;
par[u] = v;
res += w;
}
void sub1(){
for (int i = 0; i < n; i++) par[i] = i;
vector<ii> eg;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (i == j) continue;
eg.push_back({i, j, p[j] % p[i]});
}
}
sort(eg.begin(), eg.end());
for (auto x : eg){
unin(x.u, x.v, x.w);
}
cout << res;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++){
int val;
cin >> val;
p.push_back(val);
}
if (n <= 1000){
sub1();
return 0;
}
sort(p.begin(), p.end());
p.resize(unique(p.begin(), p.end()) - p.begin());
int ma = p.back();
vector<int> near(ma + 1, -1);
vector<vector<pair<int, int>>> eg(ma + 1);
for (int i = 0; i < (int) p.size(); i++){
par[i] = i;
near[p[i]] = i;
}
for (int i = ma - 1; i >= 0; i--){
if (near[i] == -1) near[i] = near[i + 1];
}
for (int i = 0; i < (int) p.size(); i++){
eg[(p[i + 1] % p[i])].push_back({i, i + 1});
for (int j = 2 * p[i]; j <= ma; j += p[i]){
int pos = near[j];
if (pos == -1) continue;
eg[(p[pos] % p[i])].push_back({i, pos});
}
}
for (int i = 0; i <= ma; i++){
for (auto x : eg[i]){
unin(x.fi, x.se, i);
}
}
cout << res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
13508 KB |
Output is correct |
2 |
Correct |
61 ms |
14280 KB |
Output is correct |
3 |
Correct |
73 ms |
14536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
14024 KB |
Output is correct |
2 |
Correct |
61 ms |
14280 KB |
Output is correct |
3 |
Correct |
76 ms |
14280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
14532 KB |
Output is correct |
2 |
Correct |
58 ms |
12996 KB |
Output is correct |
3 |
Correct |
72 ms |
12996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
38384 KB |
Output is correct |
2 |
Correct |
96 ms |
66896 KB |
Output is correct |
3 |
Correct |
57 ms |
50880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
29788 KB |
Output is correct |
2 |
Correct |
59 ms |
50900 KB |
Output is correct |
3 |
Correct |
41 ms |
24504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
50092 KB |
Output is correct |
2 |
Correct |
113 ms |
91108 KB |
Output is correct |
3 |
Correct |
54 ms |
47804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
9540 KB |
Output is correct |
2 |
Correct |
117 ms |
85676 KB |
Output is correct |
3 |
Correct |
55 ms |
52388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
287960 KB |
Output is correct |
2 |
Correct |
842 ms |
633496 KB |
Output is correct |
3 |
Correct |
140 ms |
291724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
292548 KB |
Output is correct |
2 |
Correct |
1024 ms |
749032 KB |
Output is correct |
3 |
Correct |
223 ms |
348156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
276920 KB |
Output is correct |
2 |
Correct |
838 ms |
635788 KB |
Output is correct |
3 |
Correct |
58 ms |
53440 KB |
Output is correct |