#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;
}
void process() {
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';
}
signed main() {
if(fopen("test.inp","r")) {
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
}
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// clock_t start = clock();
int ntests = 1;
// cin >> ntests;
while(ntests--) process();
// cerr << "Time elapsed: " << clock() - start << " ms!\n";
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen("test.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen("test.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
362580 KB |
Output is correct |
2 |
Correct |
74 ms |
364628 KB |
Output is correct |
3 |
Correct |
68 ms |
362580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
242772 KB |
Output is correct |
2 |
Correct |
172 ms |
363088 KB |
Output is correct |
3 |
Correct |
64 ms |
362708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
362576 KB |
Output is correct |
2 |
Correct |
62 ms |
362324 KB |
Output is correct |
3 |
Correct |
63 ms |
362580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
264660 KB |
Output is correct |
2 |
Correct |
98 ms |
294004 KB |
Output is correct |
3 |
Correct |
66 ms |
269928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
256660 KB |
Output is correct |
2 |
Correct |
71 ms |
277176 KB |
Output is correct |
3 |
Correct |
63 ms |
258828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
276216 KB |
Output is correct |
2 |
Correct |
109 ms |
305052 KB |
Output is correct |
3 |
Correct |
64 ms |
268212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
246140 KB |
Output is correct |
2 |
Correct |
110 ms |
301856 KB |
Output is correct |
3 |
Correct |
63 ms |
268016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
374556 KB |
Output is correct |
2 |
Correct |
502 ms |
579660 KB |
Output is correct |
3 |
Correct |
111 ms |
377024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
374464 KB |
Output is correct |
2 |
Correct |
626 ms |
663816 KB |
Output is correct |
3 |
Correct |
117 ms |
380900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
364628 KB |
Output is correct |
2 |
Correct |
650 ms |
657432 KB |
Output is correct |
3 |
Correct |
65 ms |
269892 KB |
Output is correct |