#include <bits/stdc++.h>
#define N 100005
#define M 10000005
using namespace std;
int arr[N];
int par[N];
int nxt[M];
int pos[M];
vector<pair<int,int>> v[M];
int find(int a){
if(a == par[a])return a;
return par[a] = find(par[a]);
}
bool unite(int a,int b){
a = find(a);
b = find(b);
if(a == b)return 0;
par[b] = a;
return 1;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
cin >> n;
int maxi = 0;
for(int i=1;i<=n;i++){
cin >> arr[i];
pos[arr[i]] = i;
maxi = max(maxi,arr[i]);
}
vector<int> nums;
for(int i=maxi;i>=0;i--){
if(pos[i]){
nxt[i] = i;
nums.push_back(i);
}
else nxt[i] = nxt[i+1];
}
for(auto u:nums){
for(int i=u;i<=maxi;i+=u){
int p = nxt[i+(i==u)];
if(p)
v[p%u].push_back({pos[u],pos[p]});
}
}
vector<pair<int,pair<int,int>>> edges;
for(int i=0;i<=maxi;i++){
for(auto u:v[i]){
edges.push_back({i,u});
}
}
for(int i=0;i<n;i++){
par[i] = i;
}
long long ans = 0;
for(auto u:edges){
if(unite(u.second.first,u.second.second))ans+=u.first;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
278252 KB |
Output is correct |
2 |
Correct |
418 ms |
360788 KB |
Output is correct |
3 |
Correct |
225 ms |
278956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
235696 KB |
Output is correct |
2 |
Runtime error |
1556 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
278636 KB |
Output is correct |
2 |
Correct |
228 ms |
276844 KB |
Output is correct |
3 |
Correct |
234 ms |
278764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
254 ms |
279368 KB |
Output is correct |
2 |
Correct |
473 ms |
333332 KB |
Output is correct |
3 |
Correct |
326 ms |
319372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
175 ms |
248592 KB |
Output is correct |
2 |
Correct |
344 ms |
317036 KB |
Output is correct |
3 |
Correct |
243 ms |
276296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
380 ms |
315772 KB |
Output is correct |
2 |
Correct |
587 ms |
399520 KB |
Output is correct |
3 |
Correct |
303 ms |
289200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
247108 KB |
Output is correct |
2 |
Correct |
599 ms |
411224 KB |
Output is correct |
3 |
Correct |
312 ms |
294212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
398 ms |
354136 KB |
Output is correct |
2 |
Runtime error |
2118 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
414 ms |
358612 KB |
Output is correct |
2 |
Runtime error |
2555 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
257 ms |
314212 KB |
Output is correct |
2 |
Runtime error |
2424 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |