#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
int suff[N];
int pos[N];
struct DSU {
vector<int> e;
DSU(int n) {
e=vector<int>(n,-1);
}
int get(int a) {
if(e[a] < 0)return a;
return e[a]=get(e[a]);
}
int unite(int a, int b) {
a=get(a),b=get(b);
if(a==b)return 0;
if(-e[a] > -e[b])swap(a,b);
e[b]+=e[a];
e[a] = b;
return 1;
}
};
int main () {
memset(suff, -1, sizeof suff);
int n;
cin >> n;
set<int> s;
for(int i = 0;i<n;i++) {
int v;
cin >> v;
s.insert(v);
}
vector<int> v(s.begin(), s.end());
{
int cnt=0;
for(int i:v) {
suff[i] =i;
pos[i] = cnt++;
}
}
for(int i = N-2;i>=0;i--) {
if(suff[i] == -1)suff[i] = suff[i+1];
}
vector<pair<int,int>> e[N];
for(int i:v) {
for(int j = i;j<N;j+=i) {
if(suff[j] == j) {
e[0].push_back({j, i});
if(suff[j+1]!=-1) {
e[suff[j+1]-j].push_back({suff[j+1], i});
}
}
else if(suff[j]!=-1){
e[suff[j]-j].push_back({suff[j], i});
}
}
}
long long sum=0;
DSU d(n);
for(int i = 0;i<N;i++) {
for(auto [u, x]:e[i]) {
sum+=i*d.unite(pos[u], pos[x]);
}
}
cout << sum << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
31 ms |
59988 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
29532 KB |
Output is correct |
2 |
Runtime error |
32 ms |
59424 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
43 ms |
59920 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
48112 KB |
Output is correct |
2 |
Correct |
142 ms |
79416 KB |
Output is correct |
3 |
Correct |
98 ms |
58724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
34652 KB |
Output is correct |
2 |
Correct |
79 ms |
56200 KB |
Output is correct |
3 |
Correct |
79 ms |
48208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
61752 KB |
Output is correct |
2 |
Correct |
171 ms |
96168 KB |
Output is correct |
3 |
Correct |
106 ms |
56956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
36220 KB |
Output is correct |
2 |
Correct |
160 ms |
99460 KB |
Output is correct |
3 |
Correct |
103 ms |
58904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
83 ms |
74068 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
79 ms |
74052 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
42 ms |
65276 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |