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
2 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
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, and . 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 choices. The arrays may contain duplicates and need not be sorted.
Input
The first line contains integers , the lengths of and .
The second line contains integers .
The third line contains integers .
Output
Print one integer: the minimum value of over all and .
Subtasks
- Subtask 1 (20 points): ; for every element of both arrays.
- Subtask 2 (30 points): ; for every element of both arrays.
- Subtask 3 (50 points): ; for every element of both arrays.
Examples
Input
2 2
1 6
4 9
Output
2
Explanation
There are four possible pairs: gives difference , gives , gives , and gives . The minimum is , attained by choosing and .