#include<bits/stdc++.h>
using namespace std;
// #define int long long
const int sz=1e5+5;
int par[sz],nx[10000005],av[10000005];
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());
// 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(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]}});
if(arr[av[j]]%arr[i]==0) v.push_back({0,{i,av[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:28:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int i=0;i<arr.size();i++){
| ~^~~~~~~~~~~
sirni.cpp:34:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | while(now+1<arr.size() && arr[now]<=i) now++;
| ~~~~~^~~~~~~~~~~
sirni.cpp:39:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i=0;i<arr.size();i++){
| ~^~~~~~~~~~~
sirni.cpp:50: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]
50 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
78692 KB |
Output is correct |
2 |
Correct |
93 ms |
82612 KB |
Output is correct |
3 |
Correct |
45 ms |
78928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4700 KB |
Output is correct |
2 |
Correct |
804 ms |
273536 KB |
Output is correct |
3 |
Correct |
30 ms |
80080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
78676 KB |
Output is correct |
2 |
Correct |
24 ms |
70236 KB |
Output is correct |
3 |
Correct |
47 ms |
78672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
38448 KB |
Output is correct |
2 |
Correct |
635 ms |
112160 KB |
Output is correct |
3 |
Correct |
295 ms |
62260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
14224 KB |
Output is correct |
2 |
Correct |
309 ms |
60968 KB |
Output is correct |
3 |
Correct |
181 ms |
33852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
388 ms |
61492 KB |
Output is correct |
2 |
Correct |
858 ms |
112348 KB |
Output is correct |
3 |
Correct |
291 ms |
62000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
13968 KB |
Output is correct |
2 |
Correct |
757 ms |
112320 KB |
Output is correct |
3 |
Correct |
271 ms |
63140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
251 ms |
104500 KB |
Output is correct |
2 |
Correct |
4323 ms |
474484 KB |
Output is correct |
3 |
Correct |
283 ms |
105264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
303 ms |
129488 KB |
Output is correct |
2 |
Runtime error |
858 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
81964 KB |
Output is correct |
2 |
Correct |
4635 ms |
474304 KB |
Output is correct |
3 |
Correct |
286 ms |
63788 KB |
Output is correct |