#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld double
#define int ll
#define usaco(fname) freopen(#fname ".in","r",stdin);freopen(#fname ".out","w",stdout);
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
// const ll INF = 1e18;
const int INF = 1e9;
const int mod = 1e9+7;
const lld PI = acos(-1.0);
int di[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dj[] = {0, 0, 1, -1, 1, -1, 1, -1};
#define debug(x) cout << #x << ": " << x << endl;
#define add(a, b) a += b, a %= mod
#define mul(a, b) ((a % mod) * (b % mod)) % mod
#define all(x) x.begin(),x.end()
struct DSU {
vector<int> p, sz, mn, mx;
DSU(int n) {
p = vector<int>(n);
sz = vector<int>(n, 1);
// mn = vector<int>(n);
for(int i = 0; i < n; i++) p[i] = i, sz[i] = 1;
}
int find(int a) {
if (a == p[a]) return p[a];
return p[a] = find(p[a]);
}
int join(int a, int b) {
int r1 = find(a);
int r2 = find(b);
if (r1 == r2) return 0;
p[r1] = r2;
sz[r2] += sz[r1];
return 1;
}
};
void solve() {
int n;cin>>n;
vector<int> a(n);
DSU dsu(n);
for (int i = 0; i < n; i++) cin >> a[i];
sort(all(a));
vector<tuple<int,int,int>> bruh;
for (int i = 0; i < n; i++) {
for (int j = i-1; j >= 0; j--) {
bruh.push_back({a[i]%a[j], j, i});
}
}
sort(all(bruh));
// for (auto [x, y, z] : bruh) cout << x << " " << y << " " << z << endl;
int ans = 0;
for (auto [cost, i, j] : bruh) {
if (dsu.join(i, j)) ans += cost;
}
cout << ans << endl;
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int t=1;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
12740 KB |
Output is correct |
2 |
Correct |
50 ms |
12740 KB |
Output is correct |
3 |
Correct |
42 ms |
12744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
12744 KB |
Output is correct |
2 |
Correct |
55 ms |
12740 KB |
Output is correct |
3 |
Correct |
44 ms |
12876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
12744 KB |
Output is correct |
2 |
Correct |
38 ms |
12744 KB |
Output is correct |
3 |
Correct |
43 ms |
12740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
553 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
547 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
581 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
553 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
617 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
550 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
574 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |