#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+1], mx;
vector<ii> edges[V+1];
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] != mx) {
if (a[i] == a[i+1]) continue;
int j = 2*a[i];
int k = nxt[a[i]+1];
if (a[k] < j) edges[a[k] - a[i]].emplace_back(i, k);
while (j <= mx) {
int k = nxt[j];
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 |
170 ms |
274372 KB |
Output is correct |
2 |
Correct |
204 ms |
276600 KB |
Output is correct |
3 |
Correct |
162 ms |
274368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
235228 KB |
Output is correct |
2 |
Correct |
385 ms |
274956 KB |
Output is correct |
3 |
Correct |
161 ms |
274524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
274380 KB |
Output is correct |
2 |
Correct |
160 ms |
274140 KB |
Output is correct |
3 |
Correct |
162 ms |
274484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
249332 KB |
Output is correct |
2 |
Correct |
188 ms |
276872 KB |
Output is correct |
3 |
Correct |
146 ms |
254704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
240724 KB |
Output is correct |
2 |
Correct |
158 ms |
261784 KB |
Output is correct |
3 |
Correct |
154 ms |
247668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
261620 KB |
Output is correct |
2 |
Correct |
198 ms |
291420 KB |
Output is correct |
3 |
Correct |
146 ms |
253048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
239472 KB |
Output is correct |
2 |
Correct |
192 ms |
286148 KB |
Output is correct |
3 |
Correct |
143 ms |
252676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
286932 KB |
Output is correct |
2 |
Correct |
980 ms |
483416 KB |
Output is correct |
3 |
Correct |
218 ms |
289412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
221 ms |
286880 KB |
Output is correct |
2 |
Correct |
1465 ms |
574188 KB |
Output is correct |
3 |
Correct |
243 ms |
293292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
276584 KB |
Output is correct |
2 |
Correct |
1536 ms |
569676 KB |
Output is correct |
3 |
Correct |
144 ms |
254256 KB |
Output is correct |