#include "bits/stdc++.h"
using namespace std;
#ifdef duc_debug
#include "bits/debug.h"
#else
#define debug(...)
#endif
const int maxn = 1e5 + 5;
const int N = 1e7 + 7;
int n;
vector<int> value[N];
int lab[maxn];
int nxt[N];
int find(int u) {
return lab[u] < 0 ? u : lab[u] = find(lab[u]);
}
bool is_joined(int u, int v) {
u = find(u), v = find(v);
if(u == v) return 1;
if(lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return 0;
}
struct edge {
int u, v, w;
bool operator<(const edge &o) {
return w < o.w;
}
};
vector<edge> e;
void solve() {
cin >> n;
vector<int> a;
for(int i = 1; i <= n; ++i) {
int x; cin >> x;
a.push_back(x);
lab[i] = -1;
value[x].push_back(i);
}
for(int i = N - 1; i; --i) {
nxt[i] = (value[i].empty() ? nxt[i + 1] : i);
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
long long ans = 0;
for(int i = 1; i < (int)a.size(); ++i) {
e.push_back({value[a[i]][0], value[a[i - 1]][0], a[i] % a[i - 1]});
}
for(int i = 0; i < (int)a.size(); ++i) {
for(int j = a[i]; j <= a.back(); j += a[i]) {
int val = nxt[j];
for(auto pos:value[val]) {
e.push_back({value[a[i]][0], pos, val % a[i]});
}
}
}
sort(e.begin(), e.end());
for(auto i:e) {
if(!is_joined(i.u, i.v)) {
ans += i.w;
}
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
274432 KB |
Output is correct |
2 |
Correct |
276 ms |
323720 KB |
Output is correct |
3 |
Correct |
125 ms |
274968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
274708 KB |
Output is correct |
2 |
Runtime error |
721 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
274572 KB |
Output is correct |
2 |
Correct |
122 ms |
274300 KB |
Output is correct |
3 |
Correct |
123 ms |
274708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
303648 KB |
Output is correct |
2 |
Correct |
460 ms |
377212 KB |
Output is correct |
3 |
Correct |
323 ms |
327684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
278080 KB |
Output is correct |
2 |
Correct |
317 ms |
326176 KB |
Output is correct |
3 |
Correct |
229 ms |
303208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
328 ms |
328488 KB |
Output is correct |
2 |
Correct |
563 ms |
377560 KB |
Output is correct |
3 |
Correct |
289 ms |
328204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
282192 KB |
Output is correct |
2 |
Correct |
561 ms |
376784 KB |
Output is correct |
3 |
Correct |
331 ms |
327944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
279 ms |
304040 KB |
Output is correct |
2 |
Runtime error |
1056 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
306 ms |
304080 KB |
Output is correct |
2 |
Runtime error |
934 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
278460 KB |
Output is correct |
2 |
Runtime error |
993 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |