import java.io.*;
import java.util.*;
public class sirni {
static int n, max;
static TreeSet<Integer> arr;
static ArrayList<Edge> edges = new ArrayList<>();
public static void main(String[] args) throws IOException{
BufferedReader file = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader file = new BufferedReader(new FileReader("file.in"));
//rintWriter outfile = new PrintWriter (new BufferedWriter(new FileWriter("Sirni.out")));
n = Integer.parseInt(file.readLine());
arr = new TreeSet<>();
HashMap<Integer, Integer> used = new HashMap<>();
for (int i=0; i<n; i++){
int curr = Integer.parseInt(file.readLine());
if (!used.containsKey(curr)){
used.put(curr, 0);
}
used.put(curr, used.get(curr)+1);
arr.add(curr);
max = Math.max(max, curr);
}
for (int i : arr){
findMultiples(i);
}
Collections.sort(edges);
DSU dsu = new DSU(max+1);
int ans = 0;
for (Edge curr : edges){
if (dsu.find(curr.from) != dsu.find(curr.to)){
dsu.union(curr.from, curr.to);
ans += curr.cost;
}
}
System.out.println(ans);
}
public static void findMultiples(int base){
int k = 1;
while (base*k <= max){
if (k == 1){
if (arr.higher(base*k) != null){
int val = arr.higher(base*k);
edges.add(new Edge(base, val, val % base));
}
}else{
if (arr.ceiling(base*k) != null){
int val = arr.ceiling(base*k);
edges.add(new Edge(base, val, val % base));
}
}
k++;
}
}
static class Edge implements Comparable<Edge>{
int from, to, cost;
public Edge(int from ,int to, int cost){
this.from = from;
this.to = to;
this.cost = cost;
}
public int compareTo(Edge o){
return this.cost - o.cost;
}
}
static class Node implements Comparable<Node>{
int val;
int id;
public Node(int val ,int id){
this.val = val;
this.id = id;
}
public int compareTo(Node o){
if (this.val == o.val){
return this.id - o.id;
}
return this.val - o.val;
}
}
static class DSU{
int n;
int[] parent;
int[] size;
public DSU(int n){
this.n = n;
parent = new int[n];
size = new int[n];
for (int i=0; i<n; i++){
parent[i] = i;
size[i] = 1;
}
}
public int find(int a){
if (parent[a] == a){
return a;
}
return parent[a] = find(parent[a]); // Path compression
}
public void union(int a, int b){
a = find(a);
b = find(b);
if (a == b){
// Same component
return;
}
if (size[a] < size[b]){
// Point a to b
parent[a] = b;
size[b] += size[a];
}else{
// Point b to a
parent[b] = a;
size[a] += size[b];
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
272 ms |
91544 KB |
Output is correct |
2 |
Correct |
1806 ms |
187200 KB |
Output is correct |
3 |
Correct |
416 ms |
93564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
11756 KB |
Output is correct |
2 |
Runtime error |
4112 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
91776 KB |
Output is correct |
2 |
Correct |
197 ms |
88864 KB |
Output is correct |
3 |
Correct |
313 ms |
93076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1846 ms |
76260 KB |
Output is correct |
2 |
Correct |
3962 ms |
235960 KB |
Output is correct |
3 |
Correct |
2455 ms |
119580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
765 ms |
28584 KB |
Output is correct |
2 |
Correct |
2315 ms |
116376 KB |
Output is correct |
3 |
Correct |
1712 ms |
71424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2885 ms |
131568 KB |
Output is correct |
2 |
Execution timed out |
5062 ms |
268592 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
995 ms |
35380 KB |
Output is correct |
2 |
Correct |
4776 ms |
300000 KB |
Output is correct |
3 |
Correct |
2209 ms |
113080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2686 ms |
140152 KB |
Output is correct |
2 |
Execution timed out |
5081 ms |
758328 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2622 ms |
160008 KB |
Output is correct |
2 |
Execution timed out |
5076 ms |
759252 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
977 ms |
103348 KB |
Output is correct |
2 |
Execution timed out |
5088 ms |
583376 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |