#include "bits/stdc++.h"
using namespace std;
#ifdef duc_debug
#include "bits/debug.h"
#else
#define debug(...)
#endif
const int maxn = 2e5 + 5;
const int N = 1e7 + 15;
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 ? w < o.w : u < o.u);
}
};
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);
nxt[x] = x;
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
for(int i = a.back() - 1; i >= 0; --i) {
if(!nxt[i])
nxt[i] = nxt[i + 1];
else {
for(int j = 1; j < (int)value[i].size(); ++j) {
is_joined(value[i][0], value[i][j]);
}
}
}
for(int i = 1; i < (int)a.size(); ++i) {
if(value[a[i]].size() and value[a[i - 1]].size())
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];
if(value[val].empty()) continue;
int pos = value[val][0];
e.push_back({value[a[i]][0], pos, (val % a[i])});
}
}
sort(e.begin(), e.end());
long long ans = 0;
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 |
117 ms |
274400 KB |
Output is correct |
2 |
Correct |
317 ms |
323564 KB |
Output is correct |
3 |
Correct |
129 ms |
274708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
235580 KB |
Output is correct |
2 |
Runtime error |
743 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
274528 KB |
Output is correct |
2 |
Correct |
132 ms |
274260 KB |
Output is correct |
3 |
Correct |
121 ms |
274564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
251 ms |
267824 KB |
Output is correct |
2 |
Correct |
615 ms |
341460 KB |
Output is correct |
3 |
Correct |
326 ms |
291912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
242884 KB |
Output is correct |
2 |
Correct |
383 ms |
290332 KB |
Output is correct |
3 |
Correct |
232 ms |
265524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
438 ms |
292356 KB |
Output is correct |
2 |
Correct |
775 ms |
341704 KB |
Output is correct |
3 |
Correct |
306 ms |
267884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
243840 KB |
Output is correct |
2 |
Correct |
751 ms |
340980 KB |
Output is correct |
3 |
Correct |
313 ms |
292124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
325 ms |
303144 KB |
Output is correct |
2 |
Runtime error |
1037 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
347 ms |
303244 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 |
175 ms |
278280 KB |
Output is correct |
2 |
Runtime error |
994 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |