# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1147231 | paolaparedes | 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();
long[][] coins = new long[2*N][2];
for (int i = 0; i < 2*N; i++) {
coins[i][0] = sc.nextLong();
coins[i][1] = sc.nextLong();
}
Arrays.sort(coins, (a, b) -> Long.compare(a[0], b[0]));
long costX = 0, costY = 0;
for (int i = 0; i < 2*N; i++) {
long assignedCol = (i / 2) + 1;
costX += Math.abs(coins[i][0] - assignedCol);
}
for (int i = 0; i < 2*N; i += 2) {
long y1 = coins[i][1], y2 = coins[i+1][1];
long option1 = Math.abs(y1 - 1) + Math.abs(y2 - 2);
long option2 = Math.abs(y1 - 2) + Math.abs(y2 - 1);
costY += Math.min(option1, option2);
}
System.out.println(costX + costY);
}
}