#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+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/2];
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) {
if(suff[j+1]-j < N/2)
e[suff[j+1]-j].push_back({suff[j+1], i});
}
}
else if(suff[j]!=-1){
if(suff[j]-j < N/2)
e[suff[j]-j].push_back({suff[j], i});
}
}
}
long long sum=0;
DSU d(n);
for(int i = 0;i<N/2;i++) {
for(auto [u, x]:e[i]) {
sum+=i*d.unite(pos[u], pos[x]);
}
}
cout << sum << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
162508 KB |
Output is correct |
2 |
Correct |
182 ms |
193380 KB |
Output is correct |
3 |
Correct |
86 ms |
163156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
159088 KB |
Output is correct |
2 |
Correct |
1494 ms |
710572 KB |
Output is correct |
3 |
Correct |
91 ms |
163668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
162764 KB |
Output is correct |
2 |
Correct |
90 ms |
161360 KB |
Output is correct |
3 |
Correct |
116 ms |
162808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
208 ms |
179184 KB |
Output is correct |
2 |
Correct |
401 ms |
210200 KB |
Output is correct |
3 |
Correct |
235 ms |
189316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
165856 KB |
Output is correct |
2 |
Correct |
254 ms |
186444 KB |
Output is correct |
3 |
Correct |
227 ms |
177164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
192972 KB |
Output is correct |
2 |
Correct |
474 ms |
226720 KB |
Output is correct |
3 |
Correct |
223 ms |
187256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
165748 KB |
Output is correct |
2 |
Correct |
435 ms |
230024 KB |
Output is correct |
3 |
Correct |
243 ms |
189456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
215752 KB |
Output is correct |
2 |
Correct |
1219 ms |
566192 KB |
Output is correct |
3 |
Correct |
194 ms |
218692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
220068 KB |
Output is correct |
2 |
Correct |
1359 ms |
723636 KB |
Output is correct |
3 |
Correct |
331 ms |
273380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
194764 KB |
Output is correct |
2 |
Correct |
1108 ms |
576988 KB |
Output is correct |
3 |
Correct |
226 ms |
189460 KB |
Output is correct |