#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]={},av[arr.back()+5]={};
// 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(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: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:35:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | while(now+1<arr.size() && arr[now]<=i) now++;
| ~~~~~^~~~~~~~~~~
sirni.cpp:40:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i=0;i<arr.size();i++){
| ~^~~~~~~~~~~
sirni.cpp:51: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]
51 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
78676 KB |
Output is correct |
2 |
Correct |
100 ms |
82828 KB |
Output is correct |
3 |
Correct |
76 ms |
78820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
851 ms |
276276 KB |
Output is correct |
3 |
Correct |
57 ms |
80120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
78684 KB |
Output is correct |
2 |
Correct |
50 ms |
78672 KB |
Output is correct |
3 |
Correct |
56 ms |
78928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
34816 KB |
Output is correct |
2 |
Correct |
599 ms |
107800 KB |
Output is correct |
3 |
Correct |
326 ms |
59516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
11520 KB |
Output is correct |
2 |
Correct |
271 ms |
59700 KB |
Output is correct |
3 |
Correct |
197 ms |
30004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
385 ms |
59184 KB |
Output is correct |
2 |
Correct |
812 ms |
108728 KB |
Output is correct |
3 |
Correct |
272 ms |
59952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
9796 KB |
Output is correct |
2 |
Correct |
733 ms |
108312 KB |
Output is correct |
3 |
Correct |
274 ms |
60464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
304 ms |
106064 KB |
Output is correct |
2 |
Correct |
4162 ms |
473864 KB |
Output is correct |
3 |
Correct |
329 ms |
105100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
366 ms |
129584 KB |
Output is correct |
2 |
Runtime error |
905 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
81860 KB |
Output is correct |
2 |
Correct |
4632 ms |
474012 KB |
Output is correct |
3 |
Correct |
332 ms |
59656 KB |
Output is correct |