#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]});
}
}
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 |
231 ms |
278124 KB |
Output is correct |
2 |
Correct |
336 ms |
311276 KB |
Output is correct |
3 |
Correct |
237 ms |
278636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
235524 KB |
Output is correct |
2 |
Correct |
1834 ms |
706548 KB |
Output is correct |
3 |
Correct |
247 ms |
278948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
224 ms |
278380 KB |
Output is correct |
2 |
Correct |
224 ms |
276844 KB |
Output is correct |
3 |
Correct |
226 ms |
278508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
242 ms |
254168 KB |
Output is correct |
2 |
Correct |
373 ms |
282260 KB |
Output is correct |
3 |
Correct |
262 ms |
268348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
245356 KB |
Output is correct |
2 |
Correct |
313 ms |
267628 KB |
Output is correct |
3 |
Correct |
207 ms |
250304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
298 ms |
265528 KB |
Output is correct |
2 |
Correct |
448 ms |
296040 KB |
Output is correct |
3 |
Correct |
259 ms |
261824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
240592 KB |
Output is correct |
2 |
Correct |
454 ms |
310980 KB |
Output is correct |
3 |
Correct |
255 ms |
266832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
367 ms |
328580 KB |
Output is correct |
2 |
Correct |
2317 ms |
657956 KB |
Output is correct |
3 |
Correct |
389 ms |
333932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
373 ms |
333412 KB |
Output is correct |
2 |
Runtime error |
2498 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
253 ms |
311000 KB |
Output is correct |
2 |
Correct |
2783 ms |
734520 KB |
Output is correct |
3 |
Correct |
266 ms |
268220 KB |
Output is correct |