#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,v2;
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]={};
// sort(arr.begin(),arr.end());
for(int i=0;i<arr.size();i++){
par[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 || ((v2.back().second.first!=i || v2.back().second.second!=nx[j]-1))) && (nx[j]-1>=0 && arr[nx[j]-1]%arr[i]==0)) v2.push_back({0,{i,nx[j]-1}});
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<v2.size();i++){
// cout<<v[i].first<<" "<<v[i].second.first<<" "<<v[i].second.second<<endl;
if(!check(v2[i].second.first,v2[i].second.second)){
ans+=v2[i].first;
merge(v2[i].second.first,v2[i].second.second);
}
}
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:29:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | 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<v2.size();i++){
| ~^~~~~~~~~~
sirni.cpp:57: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]
57 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
39512 KB |
Output is correct |
2 |
Correct |
75 ms |
43924 KB |
Output is correct |
3 |
Correct |
33 ms |
39696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
236 ms |
40376 KB |
Output is correct |
3 |
Correct |
37 ms |
39768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
39516 KB |
Output is correct |
2 |
Correct |
36 ms |
39508 KB |
Output is correct |
3 |
Correct |
33 ms |
39512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
22332 KB |
Output is correct |
2 |
Correct |
545 ms |
62440 KB |
Output is correct |
3 |
Correct |
204 ms |
32052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
8444 KB |
Output is correct |
2 |
Correct |
267 ms |
55424 KB |
Output is correct |
3 |
Correct |
158 ms |
21556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
344 ms |
60976 KB |
Output is correct |
2 |
Correct |
710 ms |
115228 KB |
Output is correct |
3 |
Correct |
204 ms |
34360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
10628 KB |
Output is correct |
2 |
Correct |
655 ms |
114608 KB |
Output is correct |
3 |
Correct |
195 ms |
33300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
242 ms |
66796 KB |
Output is correct |
2 |
Correct |
3567 ms |
444236 KB |
Output is correct |
3 |
Correct |
260 ms |
65796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
66616 KB |
Output is correct |
2 |
Correct |
4733 ms |
443912 KB |
Output is correct |
3 |
Correct |
325 ms |
68132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
43428 KB |
Output is correct |
2 |
Correct |
4494 ms |
439304 KB |
Output is correct |
3 |
Correct |
215 ms |
33904 KB |
Output is correct |