import java.io.*;
import java.util.*;
public class joi2019_ho_t4 {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(bufferedReader.readLine());
int[] a = new int[N + 1], b = new int[N + 1];
long ans = 0;
int[][] dp = new int[N + 1][3];
for (int i = 1; i <= 2 * N; i++) {
st = new StringTokenizer(bufferedReader.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
if (x > N) {
ans += (x - N);
x = N;
}
if (x < 1) {
ans += (1 - x);
x = 1;
}
if (y > 2) {
ans += (y - 2);
y = 2;
}
if (y < 1) {
ans += (1 - y);
y = 1;
}
dp[x][y]++;
if (i <= N) {
dp[i][1]--;
dp[i][2]--;
}
}
for (int i = 1; i <= N; i++) {
if (dp[i][1] < 0 && dp[i][2] > 0) {
int m = Math.min(-dp[i][1], dp[i][2]);
ans += m;
dp[i][1] += m;
dp[i][2] -= m;
}
if (dp[i][1] > 0 && dp[i][2] < 0) {
int m = Math.min(dp[i][1], -dp[i][2]);
ans += m;
dp[i][1] -= m;
dp[i][2] += m;
}
ans += Math.abs(dp[i][1]) + Math.abs(dp[i][2]);
dp[i + 1][1] += dp[i][1];
dp[i + 1][2] += dp[i][2];
}
System.out.println(ans);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |