#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;
vector<pii>con[Mxn];//reduce 1 dimension? so keep only 2 value
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;
vector<int>bro(mx+1,-1);//1e7
v.erase(unique(all(v)),v.end());
for(int i=0;i<v.size();i++){
bro[v[i]]=i;
if(i+1<v.size())con[v[i+1]%v[i]].pb({i,i+1});
}
for(int i=mx-1;i>=0;i--)if(bro[i]==-1)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[v[bro[j]]%x].pb({i,bro[j]});
}
for(int j=0;j<=mx;j++)for(auto i:con[j])if(merg(i.f,i.s))ans+=j;
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[v[i+1]%v[i]].pb({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++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
274516 KB |
Output is correct |
2 |
Correct |
179 ms |
303596 KB |
Output is correct |
3 |
Correct |
102 ms |
274524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
235344 KB |
Output is correct |
2 |
Correct |
1071 ms |
668680 KB |
Output is correct |
3 |
Correct |
102 ms |
275028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
274452 KB |
Output is correct |
2 |
Correct |
102 ms |
274260 KB |
Output is correct |
3 |
Correct |
103 ms |
274516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
250700 KB |
Output is correct |
2 |
Correct |
158 ms |
284164 KB |
Output is correct |
3 |
Correct |
111 ms |
264636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
241404 KB |
Output is correct |
2 |
Correct |
112 ms |
262524 KB |
Output is correct |
3 |
Correct |
93 ms |
248820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
264884 KB |
Output is correct |
2 |
Correct |
175 ms |
296636 KB |
Output is correct |
3 |
Correct |
114 ms |
260212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
239916 KB |
Output is correct |
2 |
Correct |
183 ms |
297248 KB |
Output is correct |
3 |
Correct |
109 ms |
262596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
289236 KB |
Output is correct |
2 |
Correct |
1150 ms |
633024 KB |
Output is correct |
3 |
Correct |
179 ms |
293616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
202 ms |
294164 KB |
Output is correct |
2 |
Correct |
1829 ms |
760908 KB |
Output is correct |
3 |
Correct |
288 ms |
352784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
277080 KB |
Output is correct |
2 |
Correct |
1748 ms |
638072 KB |
Output is correct |
3 |
Correct |
113 ms |
264648 KB |
Output is correct |