#include<bits/stdc++.h>
using namespace std;
// #define int long long
const int sz=1e5+5;
int par[sz];
vector<int> arr;
vector<pair<int,pair<int,int>>> v;
int find(int x){
if(par[x]==x) return x;
else return par[x]=find(par[x]);
}
void merge(int x, int y){
x=find(x),y=find(y);
par[x]=y;
}
bool check(int x, int y){
return find(x)==find(y);
}
signed main(){
int n; cin>>n;
for(int i=1;i<=n;i++){
int a; cin>>a;
arr.push_back(a);
}
sort(arr.begin(),arr.end());
arr.erase(unique(arr.begin(),arr.end()), arr.end());
int nx[arr.back()+5]={};
map<int,int> av;
// sort(arr.begin(),arr.end());
for(int i=0;i<arr.size();i++){
par[i]=i;
av[arr[i]]=i;
}
int now=0;
for(int i=0;i<=arr.back();i++){
while(now+1<arr.size() && arr[now]<=i) now++;
nx[i]=now;
// cout<<nx[i]<<" ";
}
// cout<<endl;
for(int i=0;i<arr.size();i++){
// cout<<arr[i]<<" ";
for(int j=arr[i];j<=arr.back();j+=arr[i]){
if(i==nx[j]) continue;
if(av.count(j) && arr[av[j]]%arr[i]==0) v.push_back({0,{i,av[j]}});
if(v.size()==0 || ((v.back().second.first!=i || v.back().second.second!=nx[j]))) v.push_back({arr[nx[j]]%arr[i],{i,nx[j]}});
}
}
// cout<<endl;
sort(v.begin(),v.end());
int ans=0;
for(int i=0;i<v.size();i++){
// cout<<v[i].first<<" "<<v[i].second.first<<" "<<v[i].second.second<<endl;
if(!check(v[i].second.first,v[i].second.second)){
ans+=v[i].first;
merge(v[i].second.first,v[i].second.second);
}
}
cout<<ans;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:30:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i=0;i<arr.size();i++){
| ~^~~~~~~~~~~
sirni.cpp:36:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | while(now+1<arr.size() && arr[now]<=i) now++;
| ~~~~~^~~~~~~~~~~
sirni.cpp:41:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i=0;i<arr.size();i++){
| ~^~~~~~~~~~~
sirni.cpp:52:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
39772 KB |
Output is correct |
2 |
Correct |
93 ms |
42436 KB |
Output is correct |
3 |
Correct |
36 ms |
39700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
609 ms |
41200 KB |
Output is correct |
3 |
Correct |
37 ms |
39772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
39608 KB |
Output is correct |
2 |
Correct |
34 ms |
39516 KB |
Output is correct |
3 |
Correct |
37 ms |
39832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
372 ms |
35376 KB |
Output is correct |
2 |
Correct |
1178 ms |
109928 KB |
Output is correct |
3 |
Correct |
450 ms |
34864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
9340 KB |
Output is correct |
2 |
Correct |
352 ms |
55060 KB |
Output is correct |
3 |
Correct |
343 ms |
32048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
755 ms |
60188 KB |
Output is correct |
2 |
Correct |
1563 ms |
109600 KB |
Output is correct |
3 |
Correct |
429 ms |
35372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
10624 KB |
Output is correct |
2 |
Correct |
1323 ms |
107260 KB |
Output is correct |
3 |
Correct |
405 ms |
34112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
418 ms |
70700 KB |
Output is correct |
2 |
Execution timed out |
5042 ms |
439316 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
422 ms |
70680 KB |
Output is correct |
2 |
Execution timed out |
5027 ms |
439096 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
44492 KB |
Output is correct |
2 |
Execution timed out |
5054 ms |
437760 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |