PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000448. Ghép học sinh với gia sư lập trình (Programming Tutors)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ghép học sinh với gia sư lập trình (Programming Tutors)

Programming Tutors

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

The Bruce Arden Programming Collective assigns experienced programming tutors to beginners. There are NN students and NN tutors, and each person must belong to exactly one student–tutor pair.

Travel takes place on a city grid, so the distance between locations (X,Y)(X,Y) and (U,V)(U,V) is

d=∣X−U∣+∣Y−V∣.d=|X-U|+|Y-V|.

Different people may live at the same location.

To keep the assignment fair, minimize the greatest travel distance incurred by any pair rather than the sum of all distances. Find the smallest achievable maximum distance KK over all complete assignments.

Input

The first line contains integer NN, the number of students and also the number of tutors.

The next NN lines contain integers Xi,YiX_i,Y_i, the location of student ii.

The final NN lines contain integers Uj,VjU_j,V_j, the location of tutor jj.

Output

Print one integer KK: the smallest bound on the distance of every pair in a complete assignment.

Subtasks

  • Subtask 1 (20 points): 1≤N≤81\le N\le8; −108≤Xi,Yi,Ui,Vi≤108-10^8\le X_i,Y_i,U_i,V_i\le10^8 for every 1≤i≤N1\le i\le N; coincident locations are allowed.
  • Subtask 2 (30 points): 1≤N≤181\le N\le18; −108≤Xi,Yi,Ui,Vi≤108-10^8\le X_i,Y_i,U_i,V_i\le10^8 for every 1≤i≤N1\le i\le N; coincident locations are allowed.
  • Subtask 3 (50 points): 1≤N≤1001\le N\le100; −108≤Xi,Yi,Ui,Vi≤108-10^8\le X_i,Y_i,U_i,V_i\le10^8 for every 1≤i≤N1\le i\le N; coincident locations are allowed.

Examples

Input 1

2
0 0
0 3
0 2
0 5

Output 1

2

Explanation 1

Pair student 1 with tutor 1 and student 2 with tutor 2. Both distances equal 2. Student 1 is at least 2 away from either tutor, establishing optimality.

Input 2

4
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3

Output 2

2

Explanation 2

Pair the ii-th student with the ii-th tutor. Each pair differs by 1 on both axes, giving distance 2. The last student (0,4)(0,4) is at least 2 from every tutor.

Input 3

3
0 5
5 2
4 5
3 3
5 2
5 2

Output 3

5

Explanation 3

Pair in input order, obtaining distances 3+2=53+2=5, 0+0=00+0=0, and 1+3=41+3=4. Student (0,5)(0,5) has minimum possible distance 5. The two tutors at (5,2)(5,2) are distinct people.

Input 4

2
0 0
0 5
-1 4
8 3

Output 4

10

Explanation 4

Pairing in input order gives distances 5 and 10. Swapping tutors makes the first student travel 8+3=118+3=11. These are the only two bijections, so the optimum is 10.

Adapted from BAPC 2016 Jury, CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/); rewritten for PHOJ.