#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define vl vector<ll>
#define fi first
#define se second
#define in insert
#define all(v) v.begin(), v.end()
#define bpc(x) __builtin_popcount(x)
#define endl "\n"
using namespace std;
const ll inf = 1e18;
const ll mod = 998244353;
const int LG = 21;
const int sz = 2e5 + 5;
const int MAX = 1e5;
ll mult(ll a, ll b){
return (a * b) % mod;
}
ll add(ll a, ll b){
return (a + b) % mod;
}
ll pw(ll a, ll b){
a %= mod;
ll res = 1;
while(b){
if(b & 1){
res = mult(res, a);
}
b >>= 1;
a = mult(a, a);
}
return res;
}
struct DSU{
vl e;
void init(ll n){
e.assign(n + 1, -1);
}
ll get(ll x){
if(e[x] < 0){
return x;
}
return e[x] = get(e[x]);
}
bool unite(ll a, ll b){
a = get(a);
b = get(b);
if(a == b){
return false;
}
if(e[a] > e[b]){
swap(a, b);
}
e[a] += e[b];
e[b] = a;
return true;
}
};
void solve(){
ll n, i, j, ans = 0;
DSU dsu;
cin >> n;
dsu.init(n);
vl v(n + 1);
for(i = 1; i <= n; i++){
cin >> v[i];
}
vector<array<ll, 3>> p;
for(i = 1; i <= n; i++){
for(j = i + 1; j <= n; j++){
ll s1 = (v[i] % v[j]), s2 = (v[j] % v[i]);
ll cost = min(s1, s2);
p.pb({cost, i, j});
}
}
sort(all(p));
for(auto [cost, u, v]: p){
if(dsu.unite(u, v)){
ans += cost;
}
}
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
// cin >> t;
while(t--){
solve();
}
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
13880 KB |
Output is correct |
2 |
Correct |
87 ms |
14020 KB |
Output is correct |
3 |
Correct |
54 ms |
13764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
14788 KB |
Output is correct |
2 |
Correct |
75 ms |
14276 KB |
Output is correct |
3 |
Correct |
59 ms |
14536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
13768 KB |
Output is correct |
2 |
Correct |
46 ms |
14024 KB |
Output is correct |
3 |
Correct |
57 ms |
14532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
506 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
416 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
423 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
416 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
434 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
403 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
422 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |