#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; --i)
#define REP(i, a, b) for(int i = (a), _b = (b); i < _b; ++i)
#define REPD(i, b, a) for(int i = (b), _a = (a); i > _a; --i)
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define all(val) val.begin(), val.end()
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(v) (int)v.size()
#define fi first
#define se second
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {return l + rng() % (r - l + 1);}
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} else return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} else return false;
}
template<class T>
T Abs(const T &x) {
return (x < 0 ? -x : x);
}
const int mod = 1e9 + 7;
const int inf = 1e9;
const int N = 1e5 + 5;
const int MAX = 1e7 + 5;
int n, a[N], nxt[MAX];
bool exist[MAX];
int par[MAX], sz[MAX];
vector<pii> g[MAX];
int mx;
int find(int u) {
if(u == par[u]) return u;
return par[u] = find(par[u]);
}
bool join(int u, int v) {
u = find(u); v = find(v);
if(u == v) return false;
if(sz[u] < sz[v]) swap(u, v);
sz[u] += sz[v];
par[v] = u;
return true;
}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
FOR(i, 1, n) {
cin >> a[i];
nxt[a[i]] = a[i];
mx = max(mx, a[i]);
exist[a[i]] = 1;
}
FORD(i, mx, 0) {
if(!exist[i]) nxt[i] = nxt[i + 1];
}
sort(a + 1, a + n + 1);
FOR(i, 1, n) {
if(!exist[a[i]]) continue;
for(int j = a[i]; j <= mx; j += a[i]) {
int res = (a[i] == j) ? a[i] + 1 : j;
if(a[i] + j > nxt[res]) g[nxt[res] % a[i]].push_back({nxt[res], a[i]});
}
exist[a[i]] = 0;
}
int ans = 0;
FOR(i, 1, mx) par[i] = i, sz[i] = 1;
FOR(i, 0, mx) {
for(auto [u, v] : g[i]) {
if(join(u, v)) ans += i;
}
}
cout << ans << '\n';
}
Compilation message
sirni.cpp:65:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
65 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
362580 KB |
Output is correct |
2 |
Correct |
88 ms |
364716 KB |
Output is correct |
3 |
Correct |
64 ms |
362576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
242724 KB |
Output is correct |
2 |
Correct |
154 ms |
363092 KB |
Output is correct |
3 |
Correct |
64 ms |
362576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
362580 KB |
Output is correct |
2 |
Correct |
67 ms |
362264 KB |
Output is correct |
3 |
Correct |
70 ms |
362580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
264580 KB |
Output is correct |
2 |
Correct |
101 ms |
293828 KB |
Output is correct |
3 |
Correct |
67 ms |
269892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
256544 KB |
Output is correct |
2 |
Correct |
75 ms |
277284 KB |
Output is correct |
3 |
Correct |
60 ms |
258832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
276204 KB |
Output is correct |
2 |
Correct |
116 ms |
305888 KB |
Output is correct |
3 |
Correct |
78 ms |
268212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
246068 KB |
Output is correct |
2 |
Correct |
112 ms |
301692 KB |
Output is correct |
3 |
Correct |
65 ms |
267848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
374752 KB |
Output is correct |
2 |
Correct |
495 ms |
582032 KB |
Output is correct |
3 |
Correct |
131 ms |
377196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
374432 KB |
Output is correct |
2 |
Correct |
652 ms |
665372 KB |
Output is correct |
3 |
Correct |
121 ms |
380904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
364708 KB |
Output is correct |
2 |
Correct |
651 ms |
657408 KB |
Output is correct |
3 |
Correct |
67 ms |
269896 KB |
Output is correct |