#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#define inputJunk ios_base::sync_with_stdio(0); cin.tie(0);
#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define debug cout<<"HERE"<<endl;
#define ell "\n"
typedef unsigned long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<int, pi> trip;
typedef pair<pll, ll> tripll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef vector<trip> vtrip;
typedef vector<tripll> vtripll;
const ll INF = 1e17+1212;
const ll P = 9973, M = 1e9+9;
const int MM = 1e5+5; int mod = 1e9+7;//99824435
int n, arr[MM], closest[MM*100];
struct DSU {
vi e; void init(int N) { e = vi(N,-1); }
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool sameSet(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) { // union by size
x = get(x), y = get(y); if (x == y) return 0;
if (e[x] > e[y]) swap(x,y);
e[x] += e[y]; e[y] = x; return 1;
}
};
template<class T> T kruskal(int N, vector<pair<T,pi>> ed) {
sort(all(ed));//reverse(all(ed));
T ans = 0; DSU D; D.init(N+1); // edges that unite are in MST
for(auto &a:ed){
if (D.unite(a.s.f,a.s.s)) {
ans += a.f;
}
}
return ans;
}
int calc(int a, int b){
if(b>a)swap(a, b);
return a%b;
}
int main(){
cin>>n;
for(int i= 0;i<n;i++){
cin>>arr[i];
closest[arr[i]] = arr[i];
}
vtrip edges;
for(int i = 10000000;i>=1;i--){
if(closest[i]==0)closest[i] = closest[i+1];
}
sort(arr, arr+n);
// for(int i = 1;i<=12;i++){
// cout<<closest[i]<<endl;
//}
for(int i = 0;i<n;i++){
for(int k = arr[i]; k<=10000000; k+=arr[i]){
if(closest[k]==0)break;
//cout<<k<<" "<<closest[k]<<" "<<closest[k+1]<<endl;
if(k==arr[i]){if(closest[k+1]==0)break;edges.pb({closest[k+1]-k, {arr[i], closest[k+1]}});}
else{
edges.pb({closest[k]-k, {arr[i], closest[k]}});
}
}
}
int ans = kruskal(10000000, edges);
cout<<ans<<ell;
}
/*CUSTOM TEST CASE 1
*/
/*CUSTOM TEST CASE 2
*/
/*CUSTOM TEST CASE 3
*/
/*Comments of Shame
- floating error (use integer division instead)
- cin vs getline
- upperbound and lowerbound returns iteratorsf
- use long long when number is big enough
- for dp invalid cases needs to be skipped
- some base cases won't work (review cow poetry)
- always check bounds, some TLE are due to incorrect bounds!
- dont mess up return types
= RESET ARRAYS!!!!!!!!!!
- USE UR BRAIN
- INF setting problems
- push vs pb
- check if things are used properly
- read the PROBLEM (directed vs undirected graph)
*/
/*
freopen("time.in","r", stdin);
freopen("time.out","w", stdout);
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
78664 KB |
Output is correct |
2 |
Correct |
682 ms |
159876 KB |
Output is correct |
3 |
Correct |
68 ms |
79200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
78708 KB |
Output is correct |
2 |
Runtime error |
956 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
78680 KB |
Output is correct |
2 |
Correct |
72 ms |
78592 KB |
Output is correct |
3 |
Correct |
74 ms |
78804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
105412 KB |
Output is correct |
2 |
Correct |
1276 ms |
217136 KB |
Output is correct |
3 |
Correct |
731 ms |
146796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
83100 KB |
Output is correct |
2 |
Execution timed out |
5063 ms |
725988 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
626 ms |
143116 KB |
Output is correct |
2 |
Correct |
2117 ms |
311652 KB |
Output is correct |
3 |
Correct |
482 ms |
127584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
88444 KB |
Output is correct |
2 |
Correct |
2041 ms |
308780 KB |
Output is correct |
3 |
Correct |
628 ms |
145716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
412 ms |
111468 KB |
Output is correct |
2 |
Runtime error |
1077 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
500 ms |
123316 KB |
Output is correct |
2 |
Runtime error |
1043 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
83276 KB |
Output is correct |
2 |
Runtime error |
1239 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |