#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using tiiii = tuple<int, int, ll, ll>;
class DSU {
private:
int n;
vector < int > parent, scale;
int GetRoot(int v) {
return parent[v] = v == parent[v] ? v : GetRoot(parent[v]);
}
public:
DSU(int n) {
this->n = n;
parent.resize(n);
iota(parent.begin(), parent.end(), 0);
scale.resize(n, 1);
}
bool Unite(int v, int u) {
v = GetRoot(v), u = GetRoot(u);
if (v == u)
return false;
if (scale[v] < scale[u])
swap(v, u);
parent[u] = v;
scale[v] += scale[u];
return true;
}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
const int P = 1e+7;
int n; cin >> n;
vector < int > p(n);
for (int i = 0; i < n; i++)
cin >> p[i];
sort(p.begin(), p.end()); p.erase(unique(p.begin(), p.end()), p.end());
n = p.size();
vector < int > nextGreater(P + 1, -1);
for (int i = 0; i < n; i++)
nextGreater[p[i]] = i;
for (int i = P; i >= 1; i--)
if (nextGreater[i] == -1)
nextGreater[i] = nextGreater[i + 1];
vector < vector < pii > > edge(P + 1);
for (int i = 0; i < n - 1; i++) {
edge[p[i + 1] % p[i]].push_back({ i, i + 1 });
for (int x = 2 * p[i]; x <= P; x += p[i]) {
int j = nextGreater[x];
if (j != -1)
edge[p[j] % p[i]].push_back({ i, j });
}
}
ll ans = 0;
DSU dsu(n);
for (int c = 0; c <= P; c++)
for (auto [i, j] : edge[c])
if (dsu.Unite(i, j))
ans += c;
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
274412 KB |
Output is correct |
2 |
Correct |
242 ms |
303796 KB |
Output is correct |
3 |
Correct |
182 ms |
274604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
396 ms |
348004 KB |
Output is correct |
2 |
Correct |
1140 ms |
669828 KB |
Output is correct |
3 |
Correct |
175 ms |
275032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
274592 KB |
Output is correct |
2 |
Correct |
159 ms |
274256 KB |
Output is correct |
3 |
Correct |
158 ms |
274624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
641 ms |
424896 KB |
Output is correct |
2 |
Runtime error |
1130 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
298928 KB |
Output is correct |
2 |
Correct |
1291 ms |
557308 KB |
Output is correct |
3 |
Correct |
1014 ms |
566964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1100 ms |
564024 KB |
Output is correct |
2 |
Runtime error |
897 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
955 ms |
567724 KB |
Output is correct |
2 |
Runtime error |
929 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
219 ms |
289264 KB |
Output is correct |
2 |
Correct |
1229 ms |
633572 KB |
Output is correct |
3 |
Correct |
236 ms |
291800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
293400 KB |
Output is correct |
2 |
Correct |
2063 ms |
747424 KB |
Output is correct |
3 |
Correct |
377 ms |
348492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
277076 KB |
Output is correct |
2 |
Correct |
1949 ms |
635492 KB |
Output is correct |
3 |
Correct |
827 ms |
481300 KB |
Output is correct |