#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<pair<int,int>> lst;
for(int i=0;i<n;i++){
cin>>arr[i];
lst.ins({arr[i],i});
}
sort(all(arr));
int mx = arr.back();
for(auto v:lst){
int c =v.first;
int i= v.second;
while(c<=mx){
auto it = lst.end();
if(c==v.first){
it = lst.upper_bound({c,inf});
}
else{
it = lst.lower_bound({c,-1});
}
if(it==lst.end()){
break;
}
// cout<<v _ " ," _ c _ *it % v <<endl;
event[ (*it).first % v.first ].pb({i,(*it).second});
c = ((*it).first / v.first +1) * v.first;
}
}
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 |
109 ms |
235344 KB |
Output is correct |
2 |
Incorrect |
109 ms |
238224 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
99 ms |
235404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
235348 KB |
Output is correct |
2 |
Incorrect |
103 ms |
235316 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
292 ms |
250576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
118 ms |
237652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
637 ms |
265008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
152 ms |
240824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
333 ms |
253008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
370 ms |
252836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
144 ms |
238500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |