#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 1e5 + 227;
const int mxM = 1e7 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
struct DSU{
vector<int> lab;
DSU(){}
DSU(int n)
{
lab.resize(n + 7 , -1);
}
int root(int u) {return lab[u] < 0 ? u : lab[u] = root(lab[u]);}
bool union_root(int u , int v)
{
u = root(u);
v = root(v);
if(u == v) return false;
if(lab[u] > lab[v]) swap(u , v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
} dsu;
struct item{
int u , v , c;
bool operator<(const item &a) {
return c < a.c;
}
};
int a[mxN];
int best[mxM];
int MAX = 1e7;
vector<item> edges;
void solve()
{
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++) cin >> a[i];
dsu = DSU(n);
sort(a + 1 , a + 1 + n);
for(int i = 1 ; i <= n ; i++) cout << a[i] << ' ';
cout << endl;
int r = n;
for(int i = MAX ; i >= 1 ; i--) {
while(a[r - 1] >= i && r >= 1) {
--r;
}
if(a[r] >= i) best[i] = r;
else best[i] = -1;
}
for(int i = 1 ; i <= n ; i++) {
if(a[i] == a[i - 1] && i >= 1) continue;
for(int j = a[i] ; j <= MAX ; j += a[i]) {
if(best[j] == -1) break;
edges.pb({i , best[j] , a[best[j]] % a[i]});
}
}
sort(edges.begin() , edges.end());
ll res = 0;
for(auto it : edges) {
if(dsu.union_root(it.u , it.v) == true) {
res += it.c;
}
}
cout << res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen(TASK".inp" , "r" , stdin);
//freopen(TASK".out" , "w" , stdout);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
39628 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
39560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
39564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
129 ms |
66392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
47 ms |
42912 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
201 ms |
90876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
46540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
158 ms |
66576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
177 ms |
66572 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
48 ms |
43120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |