#include <bits/stdc++.h>
#define N 100005
#define M 10000005
using namespace std;
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++){
int a;
cin >> a;
pos[a] = i;
maxi = max(maxi,a);
}
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){
int before = -1;
for(int i=u;i<=maxi;i+=u){
int p = nxt[i+(i==u)];
if(p && p != before)
v[p%u].push_back({pos[u],pos[p]});
before = p;
}
}
for(int i=0;i<n;i++){
par[i] = i;
}
long long ans = 0;
for(int i=0;i<=maxi;i++){
for(auto u:v[i]){
if(unite(u.first,u.second))ans+=i;
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
278124 KB |
Output is correct |
2 |
Correct |
271 ms |
278892 KB |
Output is correct |
3 |
Correct |
232 ms |
278508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
235372 KB |
Output is correct |
2 |
Correct |
548 ms |
275824 KB |
Output is correct |
3 |
Correct |
236 ms |
278380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
278380 KB |
Output is correct |
2 |
Correct |
227 ms |
277120 KB |
Output is correct |
3 |
Correct |
225 ms |
278380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
253404 KB |
Output is correct |
2 |
Correct |
381 ms |
280468 KB |
Output is correct |
3 |
Correct |
243 ms |
258252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
244844 KB |
Output is correct |
2 |
Correct |
276 ms |
265708 KB |
Output is correct |
3 |
Correct |
210 ms |
249684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
298 ms |
265140 KB |
Output is correct |
2 |
Correct |
430 ms |
292284 KB |
Output is correct |
3 |
Correct |
241 ms |
256980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
240208 KB |
Output is correct |
2 |
Correct |
410 ms |
293064 KB |
Output is correct |
3 |
Correct |
232 ms |
256856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
359 ms |
327020 KB |
Output is correct |
2 |
Correct |
1861 ms |
517508 KB |
Output is correct |
3 |
Correct |
380 ms |
329708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
383 ms |
327148 KB |
Output is correct |
2 |
Correct |
2659 ms |
634100 KB |
Output is correct |
3 |
Correct |
425 ms |
335336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
266 ms |
310996 KB |
Output is correct |
2 |
Correct |
2614 ms |
630092 KB |
Output is correct |
3 |
Correct |
259 ms |
258384 KB |
Output is correct |