#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
#include <cstdlib>
#include <cstdint>
using namespace std;
#define ll long long
#define f first
//#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
//#define int long long
#define double long double
const int mxn=1e5,inf=1e9,minf=-1e9,Mxn=1e7;
vector<int>v;
int p[mxn+10];
int mx=0;
int find(int u){return (p[u]==u)?u:p[u]=find(p[u]);}
bool merg(int a,int b){
a=find(a),b=find(b);
if(a==b)return 0;
p[a]=b;
return 1;
}
vector<pair<int,pii>>con;
int bro[Mxn+10];
int32_t main(){
fastio
int n;cin>>n;
for(int i=0;i<n;i++){
int a;cin>>a;
mx=max(mx,a);
v.pb(a);
}
for(int i=0;i<n;i++)p[i]=i;
sort(all(v));
ll ans=0;
v.erase(unique(all(v)),v.end());
for(int i=0;i<=mx;i++)bro[i]=inf;
for(int i=0;i<v.size();i++){
bro[v[i]]=i;
if(i+1<v.size())con.pb({v[i+1]%v[i],{i,i+1}});
}
for(int i=mx-1;i>=0;i--)bro[i]=min(bro[i],bro[i+1]);
for(int i=0;i<v.size();i++){
int x=v[i];
for(int j=x;j<=mx+1;j+=x)if(v[bro[j]]!=inf)con.pb({v[bro[j]]%x,{i,bro[j]}});
}
sort(all(con));
int cnt=0;
for(auto i:con)if(merg(i.s.f,i.s.s))ans+=(i.f),cnt++;
if(cnt!=v.size()-1)assert(0);
cout<<ans;
return 0;
}
/*
*/
Compilation message
sirni.cpp: In function 'int32_t main()':
sirni.cpp:65:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
sirni.cpp:67:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | if(i+1<v.size())con.pb({v[i+1]%v[i],{i,i+1}});
| ~~~^~~~~~~~~
sirni.cpp:70:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
sirni.cpp:77:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | if(cnt!=v.size()-1)assert(0);
| ~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
39772 KB |
Output is correct |
2 |
Correct |
284 ms |
90924 KB |
Output is correct |
3 |
Correct |
12 ms |
40020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Runtime error |
731 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
39768 KB |
Output is correct |
2 |
Correct |
10 ms |
39516 KB |
Output is correct |
3 |
Correct |
11 ms |
39772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
34624 KB |
Output is correct |
2 |
Correct |
606 ms |
106684 KB |
Output is correct |
3 |
Correct |
276 ms |
57908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
9980 KB |
Output is correct |
2 |
Correct |
304 ms |
57404 KB |
Output is correct |
3 |
Correct |
166 ms |
32068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
357 ms |
57904 KB |
Output is correct |
2 |
Correct |
801 ms |
106532 KB |
Output is correct |
3 |
Correct |
256 ms |
34024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
11852 KB |
Output is correct |
2 |
Correct |
823 ms |
108172 KB |
Output is correct |
3 |
Correct |
257 ms |
58676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
234 ms |
66116 KB |
Output is correct |
2 |
Runtime error |
704 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
65644 KB |
Output is correct |
2 |
Runtime error |
687 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
43024 KB |
Output is correct |
2 |
Runtime error |
882 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |