#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define file "input"
void setIO() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(file".in", "r")) {
freopen(file".in", "r", stdin);
freopen(file".out", "w", stdout);
}
}
const int INF = 2e9;
const int V = 1e7;
const int N = 1e5+5;
int n, a[N], e[N], nxt[V+100], mx;
vector<ii> edges[V+100];
int root(int u) {
return e[u] < 0 ? u : e[u] = root(e[u]);
}
bool join(int u, int v) {
u = root(u); v = root(v);
if (u == v) return 0;
if (e[u] > e[v]) swap(u, v);
e[u] += e[v];
e[v] = u;
return 1;
}
int main() {
setIO();
cin >> n;
foru(i, 1, n) {
cin >> a[i];
mx = max(mx, a[i]);
e[i] = -1;
}
sort(a+1, a+n+1);
foru(i, 1, n) {
nxt[a[i]] = i;
if (a[i] == a[i+1]) join(i, i+1);
}
ford(i, mx, 1) {
if (nxt[i] == 0) nxt[i] = nxt[i+1];
}
foru(i, 1, n) {
if (a[i] == a[i+1]) continue;
int j = a[i];
while (j <= mx) {
int k = nxt[j]; if (k == i && j < mx) k = nxt[j+1];
if (a[k] < j+a[i]) edges[a[k]-j].emplace_back(i, k);
j += a[i];
}
}
ll ans = 0;
foru(i, 0, mx) {
fore(go, edges[i]) {
if (join(go.fi, go.se)) {
ans += i;
}
}
}
cout << ans;
}
Compilation message
sirni.cpp: In function 'void setIO()':
sirni.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(file".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen(file".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
274416 KB |
Output is correct |
2 |
Correct |
191 ms |
276552 KB |
Output is correct |
3 |
Correct |
157 ms |
274344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
235304 KB |
Output is correct |
2 |
Correct |
436 ms |
274992 KB |
Output is correct |
3 |
Correct |
157 ms |
274508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
274336 KB |
Output is correct |
2 |
Correct |
157 ms |
274200 KB |
Output is correct |
3 |
Correct |
162 ms |
274440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
249332 KB |
Output is correct |
2 |
Correct |
175 ms |
277104 KB |
Output is correct |
3 |
Correct |
146 ms |
255060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
211 ms |
240732 KB |
Output is correct |
2 |
Correct |
157 ms |
261952 KB |
Output is correct |
3 |
Correct |
133 ms |
248068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
261564 KB |
Output is correct |
2 |
Correct |
198 ms |
291864 KB |
Output is correct |
3 |
Correct |
143 ms |
253360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
239404 KB |
Output is correct |
2 |
Correct |
197 ms |
286576 KB |
Output is correct |
3 |
Correct |
163 ms |
253056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
210 ms |
286932 KB |
Output is correct |
2 |
Correct |
1053 ms |
484060 KB |
Output is correct |
3 |
Correct |
250 ms |
290148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
286800 KB |
Output is correct |
2 |
Correct |
1534 ms |
574760 KB |
Output is correct |
3 |
Correct |
290 ms |
294024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
276648 KB |
Output is correct |
2 |
Correct |
1548 ms |
569608 KB |
Output is correct |
3 |
Correct |
169 ms |
254244 KB |
Output is correct |