#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 = 2e7 + 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 = 0; i < (int)a.size() - 1; ++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 + 1] % a[i]});
if(value[a[i]].empty()) continue;
for(int j = a[i] * 2; j <= a.back(); j += a[i]) {
int val = nxt[j];
if(value[val].empty()) continue;
int pos = value[val][0];
if(pos != value[a[i]][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 |
205 ms |
509268 KB |
Output is correct |
2 |
Incorrect |
416 ms |
558344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
470356 KB |
Output is correct |
2 |
Runtime error |
489 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
225 ms |
509268 KB |
Output is correct |
2 |
Correct |
212 ms |
509236 KB |
Output is correct |
3 |
Correct |
200 ms |
509500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
329 ms |
503480 KB |
Output is correct |
2 |
Correct |
628 ms |
527736 KB |
Output is correct |
3 |
Correct |
401 ms |
527388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
477892 KB |
Output is correct |
2 |
Correct |
477 ms |
525596 KB |
Output is correct |
3 |
Correct |
318 ms |
501044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
482 ms |
527900 KB |
Output is correct |
2 |
Correct |
825 ms |
577064 KB |
Output is correct |
3 |
Correct |
374 ms |
503340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
261 ms |
478796 KB |
Output is correct |
2 |
Correct |
873 ms |
576452 KB |
Output is correct |
3 |
Correct |
399 ms |
503092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
424 ms |
538676 KB |
Output is correct |
2 |
Runtime error |
722 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
450 ms |
538476 KB |
Output is correct |
2 |
Runtime error |
605 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
513480 KB |
Output is correct |
2 |
Runtime error |
610 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |