atcoder#ABC302D. [ABC302D] Impartial Gift
[ABC302D] Impartial Gift
Score : points
Problem Statement
Takahashi has decided to give one gift to Aoki and one gift to Snuke. There are candidates of gifts for Aoki, and their values are . There are candidates of gifts for Snuke, and their values are .
Takahashi wants to choose gifts so that the difference in values of the two gifts is at most .
Determine if he can choose such a pair of gifts. If he can, print the maximum sum of values of the chosen gifts.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
If he can choose gifts to satisfy the condition, print the maximum sum of values of the chosen gifts. If he cannot satisfy the condition, print .
2 3 2
3 10
2 5 15
8
The difference of values of the two gifts should be at most . If he gives a gift with value to Aoki and another with value to Snuke, the condition is satisfied, achieving the maximum possible sum of values. Thus, should be printed.
3 3 0
1 3 3
6 2 7
-1
He cannot choose gifts to satisfy the condition. Note that the candidates of gifts for a person may contain multiple gifts with the same value.
1 1 1000000000000000000
1000000000000000000
1000000000000000000
2000000000000000000
Note that the answer may not fit into a -bit integer type.
8 6 1
2 5 6 5 2 1 7 9
7 2 5 5 2 4
14