#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef pair<int, int> ii;
typedef long long ll;
const int len = 1e5+2;
int cnt[100*len], nex[2][100*len], p[len], r[len];
list<ii> vec[100*len];
int fin(int i){
if (p[i] == i) return i;
return p[i] = fin(p[i]);
}
void join(int i, int j){
if (r[i] > r[j])
p[j] = i;
else{
p[i] = j;
if (r[i] == r[j]) r[j]++;
}
}
void read(int &res){
char c;
while (c = getchar(), c < '0' || c > '9'){}
res = c-'0';
while (c = getchar(), c >= '0' && c <= '9')
res = 10*res+c-'0';
}
int main(){
int n, m = 0, k = 0;
read(n);
for (int i = 0; i < n; i++){
int temp;
read(temp);
m = max(m, temp);
if (!cnt[temp]) cnt[temp] = ++k;
}
for (int i = m; i >= 1; i--){
nex[0][i] = nex[0][i+1];
if (cnt[i]) nex[0][i] = i;
nex[1][i] = nex[1][i+1];
if (cnt[i+1]) nex[1][i] = i+1;
}
for (int i = 1; i <= k; i++){
r[i] = 0;
p[i] = i;
}
for (int i = 1; i <= m; i++)
if (cnt[i])
for (int j = i; j <= m; j+=i){
if (j == i && nex[1][j] && nex[1][j] < j+i)
vec[nex[1][j]-j].pb(mp(cnt[i], cnt[nex[1][j]]));
else if (j != i && nex[0][j] && nex[0][j] < j+i);
vec[nex[0][j]-j].pb(mp(cnt[i], cnt[nex[0][j]]));
}
ll ans = 0;
for (int i = 0; i <= m; i++){
if (k == 1) break;
for (auto it : vec[i]){
if (k == 1) break;
int x = fin(it.fi), y = fin(it.se);
if (x != y)
join(x, y), ans += i, k--;
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:66:22: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
else if (j != i && nex[0][j] && nex[0][j] < j+i);
^~
sirni.cpp:67:21: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
vec[nex[0][j]-j].pb(mp(cnt[i], cnt[nex[0][j]]));
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
317272 KB |
Output is correct |
2 |
Correct |
1369 ms |
401964 KB |
Output is correct |
3 |
Correct |
286 ms |
401964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
401964 KB |
Output is correct |
2 |
Runtime error |
3439 ms |
786432 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
279 ms |
786432 KB |
Output is correct |
2 |
Correct |
280 ms |
786432 KB |
Output is correct |
3 |
Correct |
279 ms |
786432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
500 ms |
786432 KB |
Output is correct |
2 |
Correct |
1333 ms |
786432 KB |
Output is correct |
3 |
Correct |
683 ms |
786432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
241 ms |
786432 KB |
Output is correct |
2 |
Correct |
904 ms |
786432 KB |
Output is correct |
3 |
Correct |
462 ms |
786432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
924 ms |
786432 KB |
Output is correct |
2 |
Correct |
1634 ms |
786432 KB |
Output is correct |
3 |
Correct |
620 ms |
786432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
266 ms |
786432 KB |
Output is correct |
2 |
Correct |
1758 ms |
786432 KB |
Output is correct |
3 |
Correct |
734 ms |
786432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
939 ms |
786432 KB |
Output is correct |
2 |
Execution timed out |
5119 ms |
786436 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1093 ms |
786436 KB |
Output is correct |
2 |
Runtime error |
4240 ms |
786436 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
415 ms |
786436 KB |
Output is correct |
2 |
Execution timed out |
5083 ms |
786436 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |