#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define MAX 100001
#define INF INT_MAX
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define lb(a,b) lower_bound(all(a),b)
#define ub(a,b) upper_bound(all(a),b)
#define sortv(a) sort(all(a))
#define outputar(a,b){\
for(int i=0;i<b;i++){\
cout << a[i] << " ";\
}\
cout << endl;\
}
#define outputvec(a){\
for(auto x:a){\
cout << (int)x << " ";\
}\
cout << endl;\
}
#define reset(a,n,v){\
for(int i=0;i<n;i++){\
a[i]=v;\
}\
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef tuple<ll,ll,ll> tll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef double db;
typedef long double ldb;
inline void USACO(string filename){
freopen((filename+".in").c_str(),"r",stdin);
freopen((filename+".out").c_str(),"w",stdout);
}
int n,q,t=1,m,k,x,y,z,x2,y2,z2;
vector<int> a;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//int e[1001][1001];
string s1,s2,s3;
const int mod = 998244353;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
struct DSU{
vector<int> e;
DSU(int N){ e=vector<int>(N+1,-1);}
int get(int x){return e[x]<0 ? x : e[x]=get(e[x]);}
bool same_set(int x,int y){ return get(x)==get(y);}
int size(int x){return -e[get(x)];}
bool unite(int x,int y){
x=get(x);
y=get(y);
if(x==y) return false;
if(e[x]>e[y]) swap(x,y);
e[x]+=e[y]; e[y]=x;
return true;
}
};
int N=5e6;
vector<vector<pii>> edges(N);
void solve(){
cin >> n;
a.clear();
a.resize(n);
for(int i=0;i<n;i++){
cin >> a[i];
}
int mx=0;
sortv(a);
a.erase(unique(all(a)),a.end());
n=(int)a.size();
mx=a[n-1];
for(int i=0;i<n;i++){
for(int j=a[i];j<=mx;j+=a[i]){
auto it=lb(a,j);
if(it-a.begin()==i){
++it;
}
if(it!=a.end()){
if(*it-j<a[i]){
edges[*it-j].pb({it-a.begin(),i});
}
}
}
}
DSU dsu(n);
long long res=0;
for(int i=0;i<N;i++){
for(auto x:edges[i]){
res+=dsu.unite(x.ff,x.ss)*i;
}
}
cout << res << "\n";
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("input13.txt","r",stdin);
//cin >> t;
ll cnt1=1;
while(t--){
solve();
cnt1++;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
117928 KB |
Output is correct |
2 |
Correct |
86 ms |
120404 KB |
Output is correct |
3 |
Correct |
60 ms |
117924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
117932 KB |
Output is correct |
2 |
Correct |
449 ms |
118608 KB |
Output is correct |
3 |
Correct |
57 ms |
118096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
117996 KB |
Output is correct |
2 |
Correct |
55 ms |
117680 KB |
Output is correct |
3 |
Correct |
58 ms |
118080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
128384 KB |
Output is correct |
2 |
Correct |
352 ms |
155560 KB |
Output is correct |
3 |
Correct |
197 ms |
133448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
119720 KB |
Output is correct |
2 |
Correct |
148 ms |
140372 KB |
Output is correct |
3 |
Correct |
142 ms |
128268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
140840 KB |
Output is correct |
2 |
Correct |
446 ms |
170204 KB |
Output is correct |
3 |
Correct |
177 ms |
132208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
121392 KB |
Output is correct |
2 |
Correct |
391 ms |
164912 KB |
Output is correct |
3 |
Correct |
167 ms |
131392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
131188 KB |
Output is correct |
2 |
Correct |
1620 ms |
327016 KB |
Output is correct |
3 |
Correct |
162 ms |
133580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
131212 KB |
Output is correct |
2 |
Correct |
2377 ms |
417772 KB |
Output is correct |
3 |
Correct |
275 ms |
137452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
120196 KB |
Output is correct |
2 |
Correct |
2001 ms |
413508 KB |
Output is correct |
3 |
Correct |
210 ms |
133336 KB |
Output is correct |