#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld double
#define int ll
#define usaco(fname) freopen(#fname ".in","r",stdin);freopen(#fname ".out","w",stdout);
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
// const ll INF = 1e18;
const int INF = 1e9;
const int mod = 1e9+7;
const lld PI = acos(-1.0);
int di[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dj[] = {0, 0, 1, -1, 1, -1, 1, -1};
#define debug(x) cout << #x << ": " << x << endl;
#define add(a, b) a += b, a %= mod
#define mul(a, b) ((a % mod) * (b % mod)) % mod
#define all(x) x.begin(),x.end()
struct DSU {
vector<int> p, sz, mn, mx;
DSU(int n) {
p = vector<int>(n);
// sz = vector<int>(n, 1);
// mn = vector<int>(n);
for(int i = 0; i < n; i++) p[i] = i;
}
int find(int a) {
if (a == p[a]) return p[a];
return p[a] = find(p[a]);
}
int join(int a, int b) {
int r1 = find(a);
int r2 = find(b);
if (r1 == r2) return 0;
p[r1] = r2;
// sz[r2] += sz[r1];
return 1;
}
};
const int N = 1e7+69;
vector<pair<int, int>> bruh[N];
int nxt[N];
const int D = 1e5+5;
int a[D];
void solve() {
int _n;cin>>_n;
// vector<int> a(_n);
// DSU dsu(n);
for (int i = 0; i < _n; i++) cin >> a[i];
sort(a, a+_n);
fill(nxt, nxt+N, -1);
// vector<vector<pair<int,int>>> bruh(N,vector<pair<int,int>>());
// vector<int> nxt(N, -1);
int n = unique(a, a+_n) - a;
int mx = 0;
for (int i = 0; i < n; i++) {
nxt[a[i]] = i;
mx = max(mx, a[i]);
}
// int mx = *max_element(all(a));
for (int i = mx-1; i >= 0; i--) {
if (nxt[i] == -1) nxt[i] = nxt[i+1];
}
for (int i = 0; i+1 < n; i++) {
bruh[a[i+1]%a[i]].push_back({i+1, i});
for (int j = a[i]; j <= mx; j+=a[i]) {
bruh[a[nxt[j]]%a[i]].push_back({nxt[j], i});
}
}
DSU dsu(n);
int ans = 0;
for (int i = 0; i < mx; i++) {
for (auto &[u, v] : bruh[i]) {
if (dsu.join(u, v)) ans += i;
}
}
// // for (auto [x, y, z] : bruh) cout << x << " " << y << " " << z << endl;
// int ans = 0;
cout << ans << endl;
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int t=1;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
313684 KB |
Output is correct |
2 |
Correct |
195 ms |
370516 KB |
Output is correct |
3 |
Correct |
142 ms |
313936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
313684 KB |
Output is correct |
2 |
Runtime error |
589 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
313684 KB |
Output is correct |
2 |
Correct |
135 ms |
313432 KB |
Output is correct |
3 |
Correct |
138 ms |
313688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
335848 KB |
Output is correct |
2 |
Correct |
227 ms |
392012 KB |
Output is correct |
3 |
Correct |
183 ms |
361020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
317900 KB |
Output is correct |
2 |
Correct |
186 ms |
359128 KB |
Output is correct |
3 |
Correct |
197 ms |
336080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
211 ms |
361024 KB |
Output is correct |
2 |
Correct |
280 ms |
428168 KB |
Output is correct |
3 |
Correct |
190 ms |
353932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
321440 KB |
Output is correct |
2 |
Correct |
276 ms |
427340 KB |
Output is correct |
3 |
Correct |
183 ms |
356380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
208 ms |
341388 KB |
Output is correct |
2 |
Runtime error |
902 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
350796 KB |
Output is correct |
2 |
Runtime error |
660 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
318736 KB |
Output is correct |
2 |
Runtime error |
739 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |