#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 +25 ,
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;
unordered_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));
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 +1) * 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 |
113 ms |
235324 KB |
Output is correct |
2 |
Correct |
120 ms |
237912 KB |
Output is correct |
3 |
Correct |
115 ms |
235604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
235376 KB |
Output is correct |
2 |
Correct |
107 ms |
235860 KB |
Output is correct |
3 |
Correct |
116 ms |
235628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
235504 KB |
Output is correct |
2 |
Correct |
102 ms |
235344 KB |
Output is correct |
3 |
Correct |
105 ms |
235544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
311 ms |
255880 KB |
Output is correct |
2 |
Correct |
779 ms |
281088 KB |
Output is correct |
3 |
Correct |
363 ms |
257628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
238204 KB |
Output is correct |
2 |
Correct |
227 ms |
258900 KB |
Output is correct |
3 |
Correct |
321 ms |
252748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
631 ms |
267168 KB |
Output is correct |
2 |
Correct |
1046 ms |
296296 KB |
Output is correct |
3 |
Correct |
384 ms |
257972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
241828 KB |
Output is correct |
2 |
Correct |
846 ms |
288828 KB |
Output is correct |
3 |
Correct |
365 ms |
256088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
416 ms |
257556 KB |
Output is correct |
2 |
Correct |
3726 ms |
453464 KB |
Output is correct |
3 |
Correct |
457 ms |
259732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
409 ms |
258060 KB |
Output is correct |
2 |
Execution timed out |
5062 ms |
524404 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
239440 KB |
Output is correct |
2 |
Correct |
4796 ms |
537144 KB |
Output is correct |
3 |
Correct |
418 ms |
259812 KB |
Output is correct |