#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define TRACE(x) cerr << #x << " = " << x << endl
#define REP(i, n) for (int i=0; i<n; i++)
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
typedef long long ll;
typedef pair<int, int> P;
#define X first
#define Y second
const int MAXN = 1<<19, MAXM = 1e7 + 5;
vector <P> V[MAXM];
int najbliz[MAXM];
int p[MAXN];
int n;
int root[MAXN], rnk[MAXN];
int bio[MAXM], ozn[MAXM];
int find(int x)
{
if (root[x] == -1) return x;
return root[x] = find(root[x]);
}
void merge(int a, int b)
{
a = find(a);
b = find(b);
assert(a != b);
if (rnk[a] > rnk[b]) root[b] = a;
else if (rnk[b] > rnk[a]) root[a] = b;
else {
rnk[a]++;
root[b] = a;
}
}
int main()
{
scanf("%d", &n);
memset(ozn, -1, sizeof ozn);
REP(i, n) {
scanf("%d", &p[i]);
najbliz[p[i]] = p[i];
if (ozn[p[i]] == -1)
ozn[p[i]] = i;
}
for (int i=MAXM-2; i>=0; i--)
if (!najbliz[i])
najbliz[i] = najbliz[i+1];
REP(i, n) {
if (bio[p[i]]++) continue;
if (najbliz[p[i]+1]) {
if (2*p[i] >= MAXM || najbliz[2*p[i]] != najbliz[p[i]+1])
V[najbliz[p[i]+1] - p[i]].push_back(P(i, ozn[najbliz[p[i]+1]]));
}
for (int j=2*p[i]; j<MAXM && najbliz[j]; j+=p[i])
if (j + p[i] >= MAXM || najbliz[j+p[i]] != najbliz[j])
V[najbliz[j]-j].push_back(P(i, ozn[najbliz[j]]));
}
memset(root, -1, sizeof root);
ll rje = 0;
REP(i, MAXM)
REP(j, (int) V[i].size())
if (find(V[i][j].X) != find(V[i][j].Y)) {
merge(V[i][j].X, V[i][j].Y);
rje += i;
}
REP(i, n) if (ozn[p[i]] == i) { assert(find(i) == find(0)); }
printf("%lld\n", rje);
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
sirni.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | scanf("%d", &p[i]);
| ~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
211 ms |
319176 KB |
Output is correct |
2 |
Correct |
235 ms |
320068 KB |
Output is correct |
3 |
Correct |
193 ms |
319636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
194 ms |
315740 KB |
Output is correct |
2 |
Correct |
397 ms |
316740 KB |
Output is correct |
3 |
Correct |
207 ms |
319616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
319512 KB |
Output is correct |
2 |
Correct |
200 ms |
317820 KB |
Output is correct |
3 |
Correct |
208 ms |
319736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
263 ms |
329988 KB |
Output is correct |
2 |
Correct |
383 ms |
356400 KB |
Output is correct |
3 |
Correct |
279 ms |
335252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
226 ms |
321188 KB |
Output is correct |
2 |
Correct |
304 ms |
342004 KB |
Output is correct |
3 |
Correct |
251 ms |
328320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
345 ms |
341384 KB |
Output is correct |
2 |
Correct |
399 ms |
367576 KB |
Output is correct |
3 |
Correct |
276 ms |
333088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
217 ms |
320016 KB |
Output is correct |
2 |
Correct |
387 ms |
367000 KB |
Output is correct |
3 |
Correct |
270 ms |
332248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
368356 KB |
Output is correct |
2 |
Correct |
1406 ms |
568500 KB |
Output is correct |
3 |
Correct |
340 ms |
371096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
328 ms |
368092 KB |
Output is correct |
2 |
Correct |
2464 ms |
661216 KB |
Output is correct |
3 |
Correct |
370 ms |
374848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
244 ms |
352252 KB |
Output is correct |
2 |
Correct |
2631 ms |
651684 KB |
Output is correct |
3 |
Correct |
291 ms |
334968 KB |
Output is correct |