# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1148128 | pereira_oliver | Coin Collecting (JOI19_ho_t4) | Java | 0 ms | 0 KiB |
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
List<int[]> coins = new ArrayList<>();
for (int i = 0; i < 2 * N; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
coins.add(new int[]{x, y});
}
List<int[]> targets = new ArrayList<>();
for (int x = 1; x <= N; x++) {
targets.add(new int[]{x, 1});
targets.add(new int[]{x, 2});
}
coins.sort((a, b) -> Integer.compare(a[0], b[0]));
targets.sort((a, b) -> Integer.compare(a[0], b[0]));
long totalOps = 0;
for (int i = 0; i < 2 * N; i++) {
int[] coin = coins.get(i);
int[] target = targets.get(i);
totalOps += Math.abs((long) coin[0] - target[0]) + Math.abs((long) coin[1] - target[1]);
}
System.out.println(totalOps);
}
}