#include <bits/stdc++.h>
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define REV(i, b, a) for(int i = (b); i >= (a); --i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define ll long long
#define fi first
#define se second
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n;
vector<int> ve;
int fa[N];
int root(int u) {
if(u == fa[u]) return u;
return fa[u] = root(fa[u]);
}
bool join(int u, int v) {
u = root(u); v = root(v);
if(u == v) return false;
fa[u] = v;
return true;
}
void solve(int tc) {
cin >> n;
REP(i, n) {
int x;
cin >> x;
ve.emplace_back(x);
}
sort(ve.begin(), ve.end());
ve.resize(unique(ve.begin(), ve.end()) - ve.begin());
n = ve.size();
REP(i, n) fa[i] = i;
int lim = ve.back();
vector<int> pos(lim + 5, -1);
REP(i, n) {
pos[ve[i]] = i;
}
REV(i, lim - 1, 0) {
if(pos[i] == -1) pos[i] = pos[i + 1];
}
vector<vector<pair<int, int>>> edge(lim);
REP(i, n - 1) {
edge[ve[i + 1] % ve[i]].emplace_back(i, i + 1);
for(int j = 2 * ve[i]; j <= lim; j += ve[i]) {
int nxt = pos[j];
edge[ve[nxt] % ve[i]].emplace_back(i, nxt);
}
}
int ans = 0;
FOR(i, 0, lim - 1) {
for(auto e : edge[i]) {
if(join(e.fi, e.se)) {
ans += i;
}
}
}
cout << ans << '\n';
return;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for(int i = 1; i <= tc; ++i) solve(i);
return (0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
313416 KB |
Output is correct |
2 |
Correct |
217 ms |
369436 KB |
Output is correct |
3 |
Correct |
111 ms |
312696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
848 KB |
Output is correct |
2 |
Runtime error |
889 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
313492 KB |
Output is correct |
2 |
Correct |
98 ms |
312904 KB |
Output is correct |
3 |
Correct |
113 ms |
313672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
56548 KB |
Output is correct |
2 |
Correct |
146 ms |
116536 KB |
Output is correct |
3 |
Correct |
80 ms |
79788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
35664 KB |
Output is correct |
2 |
Correct |
100 ms |
77260 KB |
Output is correct |
3 |
Correct |
55 ms |
40020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
86164 KB |
Output is correct |
2 |
Correct |
173 ms |
153488 KB |
Output is correct |
3 |
Correct |
76 ms |
76700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
14016 KB |
Output is correct |
2 |
Correct |
191 ms |
149756 KB |
Output is correct |
3 |
Correct |
80 ms |
79352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
341188 KB |
Output is correct |
2 |
Runtime error |
848 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
351424 KB |
Output is correct |
2 |
Runtime error |
868 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
318304 KB |
Output is correct |
2 |
Runtime error |
858 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |