#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);
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];
}
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;
for(auto pos:value[val]) {
if(pos == value[a[i]][0]) continue;
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 |
134 ms |
274480 KB |
Output is correct |
2 |
Correct |
273 ms |
323488 KB |
Output is correct |
3 |
Correct |
115 ms |
274712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
235460 KB |
Output is correct |
2 |
Runtime error |
737 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
274436 KB |
Output is correct |
2 |
Correct |
119 ms |
274256 KB |
Output is correct |
3 |
Correct |
120 ms |
274620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
216 ms |
267924 KB |
Output is correct |
2 |
Correct |
435 ms |
341520 KB |
Output is correct |
3 |
Correct |
302 ms |
291872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
242884 KB |
Output is correct |
2 |
Correct |
293 ms |
290300 KB |
Output is correct |
3 |
Correct |
196 ms |
265272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
317 ms |
292364 KB |
Output is correct |
2 |
Correct |
532 ms |
341720 KB |
Output is correct |
3 |
Correct |
249 ms |
267776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
243760 KB |
Output is correct |
2 |
Correct |
562 ms |
341028 KB |
Output is correct |
3 |
Correct |
271 ms |
292104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
271 ms |
303148 KB |
Output is correct |
2 |
Runtime error |
1084 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
292 ms |
303152 KB |
Output is correct |
2 |
Runtime error |
956 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
278360 KB |
Output is correct |
2 |
Runtime error |
1095 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |