#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=2e5,inf=1e9,minf=-1e9;
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;
struct num{
int x;
vector<int>wall;
void init(int j){
int cur=0,g=x;
wall.pb(0);
for(int i=x;i<=mx+1;i+=x){
int l=((wall.empty())?0LL:wall.back()),r=v.size()-1;
int pos=inf;
while(l<=r){
int mid=l+(r-l)/2;
if(v[mid]>=i)r=mid-1,pos=min(pos,mid);
else l=mid+1;
}
if(pos==inf)continue;
if(wall.empty()||pos!=wall.back())wall.pb(pos);
if(pos==v.size()-1)break;
}//find block?
for(int i=0;i<wall.size();i++)con.pb({v[wall[i]]%x,{j,wall[i]}});
}
}t[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<v.size();i++){
t[i].x=v[i];
t[i].init(i);
if(i+1<v.size())con.pb({v[i+1]%v[i],{i,i+1}});
}
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 member function 'void num::init(int)':
sirni.cpp:67:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | if(pos==v.size()-1)break;
| ~~~^~~~~~~~~~~~
sirni.cpp:69:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int i=0;i<wall.size();i++)con.pb({v[wall[i]]%x,{j,wall[i]}});
| ~^~~~~~~~~~~~
sirni.cpp:55:7: warning: unused variable 'cur' [-Wunused-variable]
55 | int cur=0,g=x;
| ^~~
sirni.cpp:55:13: warning: unused variable 'g' [-Wunused-variable]
55 | int cur=0,g=x;
| ^
sirni.cpp: In function 'int32_t main()':
sirni.cpp:84:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
sirni.cpp:87:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | if(i+1<v.size())con.pb({v[i+1]%v[i],{i,i+1}});
| ~~~^~~~~~~~~
sirni.cpp:92:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | if(cnt!=v.size()-1)assert(0);
| ~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
58 ms |
11828 KB |
Output is correct |
3 |
Correct |
4 ms |
7000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7004 KB |
Output is correct |
2 |
Correct |
378 ms |
8488 KB |
Output is correct |
3 |
Correct |
4 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
3 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
254 ms |
39988 KB |
Output is correct |
2 |
Correct |
794 ms |
77424 KB |
Output is correct |
3 |
Correct |
323 ms |
37548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
11744 KB |
Output is correct |
2 |
Correct |
354 ms |
70228 KB |
Output is correct |
3 |
Correct |
228 ms |
38196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
503 ms |
69236 KB |
Output is correct |
2 |
Correct |
1012 ms |
128424 KB |
Output is correct |
3 |
Correct |
307 ms |
37816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
16208 KB |
Output is correct |
2 |
Correct |
928 ms |
129008 KB |
Output is correct |
3 |
Correct |
306 ms |
37812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
287 ms |
38332 KB |
Output is correct |
2 |
Execution timed out |
5008 ms |
472044 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
285 ms |
38072 KB |
Output is correct |
2 |
Execution timed out |
5058 ms |
542388 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
12284 KB |
Output is correct |
2 |
Execution timed out |
5045 ms |
544900 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |