#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ins insert
#define pb push_back
#define putr(x) cout<<x<<endl;return;
#define all(x) x.begin(),x.end()
#define _ << " " <<
const int mod = 1e9+7 ,
sze= 1e7*2 +23 ,
inf = INT_MAX ,
L = 31 ;
// credit : aykhn
int parent[sze];
int sz[sze];
int root(int node){
if(parent[node]==node){
return node;
}
return parent[node]=root(parent[node]);
}
int unite(int a,int b){
int x = root(a);
int y = root(b);
if(x==y){
return 0;
}
if(sz[y]>sz[x]){
swap(y,x);
}
parent[y]=x;
sz[x]+=sz[y];
return 1;
}
vector<pair<int,int>> event[sze];
void opt1z(){
int n;
cin>>n;
for(int i=0;i<=(int)n+23;i++){
sz[i]=1;
parent[i]=i;
}
int ans=0;
vector<int> arr(n);
set<int> lst;
map<int,int> idx;
for(int i=0;i<n;i++){
cin>>arr[i];
idx[arr[i]]=i;
lst.ins(arr[i]);
}
sort(all(arr));
vector<int> yuxari;
int mx = arr.back();
for(auto v:lst){
int c =v;
int i= idx[v];
while(c<=mx){
auto it = lst.end();
if(c==v){
it = lst.upper_bound(c);
}
else{
it = lst.lower_bound(c);
}
if(it==lst.end()){
break;
}
// cout<<v _ " ," _ c _ *it % v <<endl;
int ib = idx[*it];
event[ (*it) % v ].pb({i,ib});
c = ((*it) / v) * v;
c+=v;
}
}
for(int i =0;i<=mx;i++){
for(auto v:event[i]){
// cout<<i _ v.first _ "=> " _ v.second<<endl;
if(unite(v.first,v.second)){
ans+=i;
}
}
}
putr(ans);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
// cin>>tt;
while(tt--){
opt1z();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
470352 KB |
Output is correct |
2 |
Correct |
228 ms |
472576 KB |
Output is correct |
3 |
Correct |
221 ms |
470356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
200 ms |
470252 KB |
Output is correct |
2 |
Correct |
218 ms |
470708 KB |
Output is correct |
3 |
Correct |
212 ms |
470420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
470356 KB |
Output is correct |
2 |
Correct |
222 ms |
470100 KB |
Output is correct |
3 |
Correct |
208 ms |
470204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
536 ms |
489548 KB |
Output is correct |
2 |
Correct |
1194 ms |
516012 KB |
Output is correct |
3 |
Correct |
560 ms |
492996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
243 ms |
472912 KB |
Output is correct |
2 |
Correct |
378 ms |
493784 KB |
Output is correct |
3 |
Correct |
471 ms |
488104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
914 ms |
501876 KB |
Output is correct |
2 |
Correct |
1394 ms |
531164 KB |
Output is correct |
3 |
Correct |
550 ms |
492712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
476860 KB |
Output is correct |
2 |
Correct |
1118 ms |
524016 KB |
Output is correct |
3 |
Correct |
534 ms |
491460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
542 ms |
492636 KB |
Output is correct |
2 |
Correct |
4560 ms |
688656 KB |
Output is correct |
3 |
Correct |
601 ms |
495692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
552 ms |
492324 KB |
Output is correct |
2 |
Execution timed out |
5069 ms |
703128 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
474196 KB |
Output is correct |
2 |
Execution timed out |
5084 ms |
738804 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |