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

#TP0000017. Hiệu nhỏ nhất giữa hai dãy (Min Difference)

HỌC THEO LỘ TRÌNH

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

2 lộ trình

Hiệu nhỏ nhất giữa hai dãy (Min Difference)

Min Difference

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given two arrays of positive integers, A=(a1,a2,…,an)A=(a_1,a_2,\ldots,a_n) and B=(b1,b2,…,bm)B=(b_1,b_2,\ldots,b_m). You must choose exactly one element from each array.

The difference between the chosen elements is the absolute value of their subtraction. Find the minimum possible difference over all n⋅mn\cdot m choices. The arrays may contain duplicates and need not be sorted.

Input

The first line contains integers n,mn,m, the lengths of AA and BB.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

The third line contains mm integers b1,b2,…,bmb_1,b_2,\ldots,b_m.

Output

Print one integer: the minimum value of ∣ai−bj∣|a_i-b_j| over all 1≤i≤n1\le i\le n and 1≤j≤m1\le j\le m.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤121\le n,m\le 12; 1≤ai,bj≤1091\le a_i,b_j\le10^9 for every element of both arrays.
  • Subtask 2 (30 points): 1≤n,m≤20001\le n,m\le 2000; 1≤ai,bj≤1091\le a_i,b_j\le10^9 for every element of both arrays.
  • Subtask 3 (50 points): 1≤n,m≤2000001\le n,m\le 200000; 1≤ai,bj≤1091\le a_i,b_j\le10^9 for every element of both arrays.

Examples

Input

2 2
1 6
4 9

Output

2

Explanation

There are four possible pairs: (1,4)(1,4) gives difference 33, (1,9)(1,9) gives 88, (6,4)(6,4) gives 22, and (6,9)(6,9) gives 33. The minimum is 22, attained by choosing 66 and 44.