#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define ff first
#define ss second
#define rep(i,s,f) for(int i = s;i < f;i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
const int INF = 1e18,N = 1e3 + 1;
const int prime = 31,m = 1e9 + 9,MOD7 = 1e9 + 7;
int binpow(int n,int k){
if(k == 0){
return 1 ;
} else if(k % 2 == 0){
int b = binpow(n,k / 2);
return b * b ;
} else{
return n * binpow(n,k - 1) ;
}
}
int p[N];
int sz[N];
int find(int n){
if(p[n] == n){
return n;
}
return p[n] = find(p[n]);
}
void unin(int a,int b){
a = find(a);
b = find(b);
if(sz[a] > sz[b]){
swap(a,b);
}
sz[b] += sz[a];
p[a] = b;
}
void solve(){
int n;
cin >> n;
int a[n];
for(int i = 0;i < n;i++){
p[i] = i;
sz[i] = 1;
cin >> a[i];
}
if(n <= 1000){
vector<tuple<int,int,int>> g;
for(int i = 0;i < n;i++){
for(int j = i + 1;j < n;j++){
int mn = a[i] % a[j];
mn = min(mn,a[j] % a[i]);
g.pb({mn,i,j});
}
}
sort(all(g));
int sum = 0;
for(auto [cost,a,b] : g){
if(find(a) != find(b)){
unin(a,b);
sum += cost;
}
}
cout << sum;
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int t = 1;
//~ cin >> t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
13200 KB |
Output is correct |
2 |
Correct |
60 ms |
12744 KB |
Output is correct |
3 |
Correct |
48 ms |
14276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
14536 KB |
Output is correct |
2 |
Correct |
63 ms |
13256 KB |
Output is correct |
3 |
Correct |
50 ms |
13768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
14276 KB |
Output is correct |
2 |
Correct |
40 ms |
14280 KB |
Output is correct |
3 |
Correct |
51 ms |
14024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
2140 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
2140 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
1116 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
2140 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
2396 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |