#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define int ll
#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());
string s[MAX],str[MAX];
//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<N){
edges[*it-j].pb({it-a.begin(),i});
}
}
}
}
DSU dsu(n);
int 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 |
52 ms |
124248 KB |
Output is correct |
2 |
Correct |
193 ms |
187608 KB |
Output is correct |
3 |
Correct |
59 ms |
124756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
124244 KB |
Output is correct |
2 |
Runtime error |
1346 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
124244 KB |
Output is correct |
2 |
Correct |
60 ms |
123988 KB |
Output is correct |
3 |
Correct |
61 ms |
124244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
145400 KB |
Output is correct |
2 |
Correct |
402 ms |
203668 KB |
Output is correct |
3 |
Correct |
197 ms |
169724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
128080 KB |
Output is correct |
2 |
Correct |
214 ms |
170844 KB |
Output is correct |
3 |
Correct |
147 ms |
145152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
169940 KB |
Output is correct |
2 |
Correct |
485 ms |
240380 KB |
Output is correct |
3 |
Correct |
189 ms |
162628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
131364 KB |
Output is correct |
2 |
Correct |
444 ms |
246304 KB |
Output is correct |
3 |
Correct |
185 ms |
167692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
150868 KB |
Output is correct |
2 |
Runtime error |
1693 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
159448 KB |
Output is correct |
2 |
Runtime error |
1401 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
128848 KB |
Output is correct |
2 |
Runtime error |
1855 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |