#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,e,i,j,ii,jj,zx,xc,f[100009],pas,msh[100009],zm[100009],fx[10000009];
vector <pair <long long, pair <long long, long long> > > P;
long long fnd(long long q){
if(msh[q]==0) return q; else return msh[q]=fnd(msh[q]);
}
void mrg(long long q, long long w){
q=fnd(q);w=fnd(w);
if(q==w) return;
pas+=e;
if(zm[q]<zm[w]) swap(q,w);
msh[w]=q;
if(zm[q]==zm[w]) zm[q]++;
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a;map <int, int> ma;
zx=0;
for(i=1; i<=a; i++){
cin>>c;
if(ma[c]!=-1){
ma[c]=-1;
zx++;f[zx]=c;
}
}
a=zx;
for(i=1; i<=a; i++){
//cin>>f[i];
fx[f[i]]=i;
}
for(i=10000000; i>=0; i--){
if(fx[i]==0) fx[i]=fx[i+1];
}
for(i=1; i<=a; i++){
for(j=f[i]; j<=10000000; j+=f[i]){
if(j==f[i]) c=fx[j+1]; else c=fx[j];
if(c==0||f[c]-j>=f[i]) continue;
zx=(f[c]-j)%f[i];
P.push_back({zx,{i,c}});
}
}
sort(P.begin(),P.end());
for(ii=0; ii<P.size(); ii++){
e=P[ii].first;c=P[ii].second.first;d=P[ii].second.second;
mrg(c,d);
}
cout<<pas;
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:44:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(ii=0; ii<P.size(); ii++){
| ~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
78924 KB |
Output is correct |
2 |
Correct |
134 ms |
84928 KB |
Output is correct |
3 |
Correct |
75 ms |
79152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
78948 KB |
Output is correct |
2 |
Correct |
348 ms |
80244 KB |
Output is correct |
3 |
Correct |
72 ms |
79088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
78920 KB |
Output is correct |
2 |
Correct |
72 ms |
78688 KB |
Output is correct |
3 |
Correct |
69 ms |
78912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
422 ms |
109244 KB |
Output is correct |
2 |
Correct |
1213 ms |
182044 KB |
Output is correct |
3 |
Correct |
548 ms |
132108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
85564 KB |
Output is correct |
2 |
Correct |
710 ms |
177636 KB |
Output is correct |
3 |
Correct |
498 ms |
108676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
788 ms |
182380 KB |
Output is correct |
2 |
Correct |
1504 ms |
280820 KB |
Output is correct |
3 |
Correct |
575 ms |
133024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
299 ms |
92832 KB |
Output is correct |
2 |
Correct |
1540 ms |
279732 KB |
Output is correct |
3 |
Correct |
481 ms |
132532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
350 ms |
133344 KB |
Output is correct |
2 |
Runtime error |
1134 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
387 ms |
133396 KB |
Output is correct |
2 |
Runtime error |
1115 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
85920 KB |
Output is correct |
2 |
Runtime error |
1198 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |