#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;
priority_queue<pair<int,ii>,vector<pair<int,ii>>,greater<pair<int,ii>>>edge;
vector<int>no(1000005);
int F(int x){
if(no[x]==x)return x;
return no[x]=F(no[x]);
}
bool merge(int x,int y){
x=F(x);
y=F(y);
if(x==y)return false;
no[x]=y;
return true;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("q.gir","r",stdin);
// freopen("q.cik","w",stdout);
int n,ans=0;
cin>>n;
for(int i=0;i<=n;i++)no[i]=i;
vector<int>A(n);
for(int i=0;i<n;i++)cin>>A[i];
sort(all(A));
vector<int>arr;
arr.pb(A[0]);
for(int i=1;i<n;i++){
if(A[i]!=A[i-1])arr.pb(A[i]);
}
map<int,vector<int>> data;
for(int i=0;i*arr[0]<=1e6;i++){
data[arr[0]*i].pb(0);
}
for(int i=1;i<arr.size();i++){
map<int,vector<int>>::iterator itr;
vector<int>& Q=(itr=--data.upper_bound(arr[i]))->second;
for(int j=0;j<Q.size();j++){
edge.push({arr[i]-itr->first,{i,Q[j]}});
}
for(int j=1;j*arr[i]<=1e6;j++){
data[arr[i]*j].pb(i);
}
}
while(!edge.empty()){
int x=edge.top().second.first;
int y=edge.top().second.second;
int w=edge.top().first;
edge.pop();
if(merge(x,y)){
ans+=w;
}
}
cout<<ans;
}
Compilation message
sirni.cpp: In function 'int32_t main()':
sirni.cpp:44:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<arr.size();i++){
~^~~~~~~~~~~
sirni.cpp:47:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<Q.size();j++){
~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
8312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
618 ms |
56192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
8312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
963 ms |
73748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
133 ms |
25652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2148 ms |
105804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1904 ms |
91052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
122 ms |
24140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
156 ms |
28488 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
10932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |