#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;
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;
}
vector<pair<int, int>> e[N];
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;
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
for(int i = 0; i < (int)a.size(); ++i) {
nxt[a[i]] = i + 1;
}
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) {
e[a[i] % a[i - 1]].push_back({i, i + 1});
}
for(int i = 0; i < (int)a.size(); ++i) {
for(int j = a[i] * 2; j <= a.back(); j += a[i]) {
int val = nxt[j];
if(!val) continue;
e[a[val - 1] % a[i]].push_back({i + 1, val});
j = a[val - 1] / a[i] * a[i];
}
}
long long ans = 0;
for(int i = 0; i <= a.back(); ++i) {
for(auto j:e[i]) {
if(!is_joined(j.first, j.second)) {
ans += i;
}
}
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
274316 KB |
Output is correct |
2 |
Correct |
153 ms |
276796 KB |
Output is correct |
3 |
Correct |
134 ms |
274516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
235344 KB |
Output is correct |
2 |
Correct |
137 ms |
275032 KB |
Output is correct |
3 |
Correct |
136 ms |
274612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
274336 KB |
Output is correct |
2 |
Correct |
131 ms |
274168 KB |
Output is correct |
3 |
Correct |
133 ms |
274512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
249284 KB |
Output is correct |
2 |
Correct |
197 ms |
275988 KB |
Output is correct |
3 |
Correct |
134 ms |
254152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
240680 KB |
Output is correct |
2 |
Correct |
160 ms |
261584 KB |
Output is correct |
3 |
Correct |
134 ms |
247452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
261528 KB |
Output is correct |
2 |
Correct |
229 ms |
291900 KB |
Output is correct |
3 |
Correct |
142 ms |
253060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
239420 KB |
Output is correct |
2 |
Correct |
220 ms |
285232 KB |
Output is correct |
3 |
Correct |
140 ms |
252628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
287184 KB |
Output is correct |
2 |
Correct |
796 ms |
483164 KB |
Output is correct |
3 |
Correct |
196 ms |
289492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
191 ms |
287700 KB |
Output is correct |
2 |
Correct |
1087 ms |
573660 KB |
Output is correct |
3 |
Correct |
223 ms |
293260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
276956 KB |
Output is correct |
2 |
Correct |
1138 ms |
569712 KB |
Output is correct |
3 |
Correct |
138 ms |
254160 KB |
Output is correct |