#include <bits/stdc++.h>
// author : aykhn
using namespace std;
typedef long long ll;
#define pb push_back
#define ins insert
#define mpr make_pair
#define all(v) v.begin(), v.end()
#define bpc __builtin_popcountll
#define pii pair<ll, ll>
#define pll pair<ll, ll>
#define fi first
#define se second
#define int ll
#define infll 0x3F3F3F3F3F3F3F3F
#define inf 0x3F3F3F3F
struct DSU
{
vector<int> e;
void init(int n)
{
e.assign(n + 1, -1);
}
int get(int x)
{
if (e[x] < 0) return x;
return e[x] = get(e[x]);
}
int tog(int x, int y)
{
return get(x) == get(y);
}
int unite(int x, int y)
{
x = get(x);
y = get(y);
if (x == y) return 0;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y];
e[y] = x;
return 1;
}
};
const int MXN = 5e5 + 5;
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n;
cin >> n;
int a[n + 1];
a[0] = 0;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
vector<array<int, 3>> e;
for (int i = 1; i <= n; i++)
{
if (a[i] == a[i - 1]) continue;
for (int j = a[i]; j <= a[n]; j += a[i])
{
int ind = lower_bound(a + 1, a + n + 1, j) - a;
if (j == a[i]) ind++;
if (ind > n) break;
e.pb({a[ind] % a[i], i, ind});
}
}
sort(all(e));
int res = 0;
DSU dsu;
dsu.init(n);
for (int i = 0; i < e.size(); i++)
{
if (dsu.unite(e[i][1], e[i][2])) res += e[i][0];
}
cout << res << '\n';
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:77:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int i = 0; i < e.size(); i++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Incorrect |
340 ms |
99960 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
692 KB |
Output is correct |
2 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
262 ms |
51936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
39 ms |
8512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
586 ms |
99992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
89 ms |
14532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
320 ms |
50900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
390 ms |
51852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
7624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |