#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define pf push_front
#define N 100010
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef short int si;
typedef array <int, 6> a6;
//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int pr[N], rk[N];
vector <int> a;
void make(int x) {pr[x] = x; rk[x] = 1;}
int fnd(int x) {if (pr[x] != x) pr[x] = fnd(pr[x]); return pr[x];}
int valer(int x, int y) {return min(a[x] % a[y], a[y] % a[x]);}
void link(int a, int b)
{
if (rk[b] > rk[a]) swap(a, b);
pr[b] = a;
rk[a] += rk[b];
}
bool cmp(pair <int, int> x, pair <int, int> y){return valer(x.F, x.S) < valer(y.F, y.S);}
int main()
{
//freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
a.resize(n);
int mx = 0;
for (int i = 0; i < n; i++) {cin >> a[i]; mx = max(mx, a[i]);}
sort(a.begin(), a.end());
a.resize(unique(a.begin(), a.end()) - a.begin());
n = sz(a);
ll ans = 0;
for (int i = 0; i < n; i++) make(i);
vector <pair <int, int> > gr; gr.clear();
for (int i = 0; i < n; i++)
{
int j = i + 1;
for (int u = a[i]; u <= mx && j < n; u += a[i])
{
while (j < n && a[j] < u) j++;
if (j != n) gr.pb({i, j});
}
}
sort(gr.begin(), gr.end(), cmp);
for (auto it : gr)
{
int x = fnd(it.F), y = fnd(it.S);
if (x == y) continue;
ans += valer(it.F, it.S);
link(x, y);
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
1681 ms |
33344 KB |
Output is correct |
3 |
Correct |
12 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Execution timed out |
5087 ms |
525924 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1796 ms |
18020 KB |
Output is correct |
2 |
Correct |
3497 ms |
34532 KB |
Output is correct |
3 |
Correct |
2053 ms |
34372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
2804 KB |
Output is correct |
2 |
Correct |
1361 ms |
33860 KB |
Output is correct |
3 |
Correct |
1423 ms |
18012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2559 ms |
34676 KB |
Output is correct |
2 |
Correct |
4660 ms |
67368 KB |
Output is correct |
3 |
Correct |
2228 ms |
18020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
340 ms |
5100 KB |
Output is correct |
2 |
Correct |
4686 ms |
67108 KB |
Output is correct |
3 |
Correct |
2086 ms |
17892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2082 ms |
18056 KB |
Output is correct |
2 |
Execution timed out |
5098 ms |
527020 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2397 ms |
18148 KB |
Output is correct |
2 |
Execution timed out |
5111 ms |
526932 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
2804 KB |
Output is correct |
2 |
Execution timed out |
5054 ms |
526892 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |