#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 1e5 + 5, inf = 1e9;
int n, a[maxn];
vector<ar<int, 2>> ed[maxn * 10];
int lab[maxn];
int finds(int u)
{
if(lab[u] < 0) return u;
return lab[u] = finds(lab[u]);
}
bool merges(int u, int v)
{
u = finds(u); v = finds(v);
if(u == v) return false;
if(lab[v] < lab[u]) swap(u, v);
lab[u] += lab[v]; lab[v] = u;
return true;
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
cin >> n;
fill(lab + 1, lab + 1 + n, -1);
set<int> se;
for(int i = 1; i <= n; ++i)
cin >> a[i], se.insert(a[i]);
int n = 0;
for(int i : se){
a[++n] = i;
}
for(int i = 1; i <= n; ++i){
if(i < n)
ed[a[i + 1] % a[i]].pb({i, i + 1});
int j = i + 1;
for(int num = 2 * a[i]; num <= a[n]; num += a[i]){
while(a[j] < num) ++j;
ed[a[j] % a[i]].pb({j, i});
}
}
ll mst = 0;
for(int i = 0; i <= a[n]; ++i){
for(auto j : ed[i]){
if(merges(j[0], j[1]))
mst += i;
}
}
cout << mst;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
45 ms |
48504 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
24064 KB |
Output is correct |
2 |
Runtime error |
1629 ms |
786432 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
45 ms |
48504 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1382 ms |
38740 KB |
Output is correct |
2 |
Correct |
1257 ms |
66624 KB |
Output is correct |
3 |
Correct |
919 ms |
48592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
26488 KB |
Output is correct |
2 |
Correct |
157 ms |
47736 KB |
Output is correct |
3 |
Correct |
957 ms |
38052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1451 ms |
50832 KB |
Output is correct |
2 |
Correct |
1454 ms |
85180 KB |
Output is correct |
3 |
Correct |
1278 ms |
47680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
221 ms |
29148 KB |
Output is correct |
2 |
Correct |
1358 ms |
84276 KB |
Output is correct |
3 |
Correct |
1030 ms |
48848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1496 ms |
85048 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1499 ms |
93636 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
120 ms |
54904 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |