#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 = (j == a[i] ? upper_bound(a + 1, a + n + 1, j) - a : lower_bound(a + 1, a + n + 1, j) - a);
if (ind > n) break;
e.pb({a[ind] % a[i], i, ind});
j = a[ind]/a[i] * a[i];
}
}
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 |
604 KB |
Output is correct |
2 |
Correct |
29 ms |
7372 KB |
Output is correct |
3 |
Correct |
3 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
9 ms |
2004 KB |
Output is correct |
3 |
Correct |
3 ms |
1052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
258 ms |
26848 KB |
Output is correct |
2 |
Correct |
977 ms |
100540 KB |
Output is correct |
3 |
Correct |
392 ms |
51012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
7116 KB |
Output is correct |
2 |
Correct |
416 ms |
100052 KB |
Output is correct |
3 |
Correct |
246 ms |
27564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
586 ms |
101272 KB |
Output is correct |
2 |
Correct |
1250 ms |
199312 KB |
Output is correct |
3 |
Correct |
319 ms |
50916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
15044 KB |
Output is correct |
2 |
Correct |
1089 ms |
199360 KB |
Output is correct |
3 |
Correct |
344 ms |
51112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
291 ms |
51408 KB |
Output is correct |
2 |
Runtime error |
1219 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
295 ms |
52384 KB |
Output is correct |
2 |
Runtime error |
1242 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
8136 KB |
Output is correct |
2 |
Runtime error |
1235 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |