#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, 3>> edge;
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)
edge.pb({a[i + 1] % a[i], i, i + 1});
int j = i + 1;
for(int num = 2 * a[i]; num <= a[n]; num += a[i]){
while(a[j] < num) ++j;
edge.pb({a[j] % a[i], j, i});
}
}
ll mst = 0;
sort(edge.begin(), edge.end());
for(auto & all : edge){
if(merges(all[1], all[2]))
mst += all[0];
}
cout << mst;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Correct |
388 ms |
49832 KB |
Output is correct |
3 |
Correct |
8 ms |
960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
640 KB |
Output is correct |
2 |
Runtime error |
866 ms |
786436 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
640 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1452 ms |
31140 KB |
Output is correct |
2 |
Correct |
2092 ms |
55628 KB |
Output is correct |
3 |
Correct |
1288 ms |
54824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
4340 KB |
Output is correct |
2 |
Correct |
543 ms |
51496 KB |
Output is correct |
3 |
Correct |
1102 ms |
30280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1777 ms |
55696 KB |
Output is correct |
2 |
Correct |
2624 ms |
104696 KB |
Output is correct |
3 |
Correct |
1579 ms |
30920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
263 ms |
8932 KB |
Output is correct |
2 |
Correct |
2555 ms |
103800 KB |
Output is correct |
3 |
Correct |
1361 ms |
30432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1648 ms |
31304 KB |
Output is correct |
2 |
Runtime error |
971 ms |
786436 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1683 ms |
31316 KB |
Output is correct |
2 |
Runtime error |
949 ms |
786436 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
4852 KB |
Output is correct |
2 |
Runtime error |
1499 ms |
786436 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |