PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#TP0000027. Quà tặng công bằng (Impartial Gift)
HỌC THEO LỘ TRÌNH
2 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Quà tặng công bằng (Impartial Gift)
Impartial Gift
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Takahashi wants to give one gift to Aoki and one gift to Snuke. There are candidate gifts for Aoki with values , and candidate gifts for Snuke with values .
He wants the values of the chosen gifts to differ by at most . Find the maximum possible sum of their values. If no valid pair exists, print -1. The arrays may contain duplicate values.
Input
- The first line contains , the numbers of candidate gifts and the maximum allowed difference.
- The second line contains integers , the gift values for Aoki.
- The third line contains integers , the gift values for Snuke.
Output
Print the maximum sum of a valid pair; print -1 if none exists.
Subtasks
- Subtask 1 (20 points): ; ; for every element.
- Subtask 2 (30 points): ; ; for every element.
- Subtask 3 (50 points): ; ; for every element.
Examples
Input
2 3 2
3 10
2 5 15
Output
8
Explanation
If Aoki receives the gift valued , Snuke may receive a gift valued or , yielding sums or ; both differences are at most . The gift valued has no compatible counterpart among . Therefore the maximum is .