atcoder#ABC302D. [ABC302D] Impartial Gift

[ABC302D] Impartial Gift

Score : 400400 points

Problem Statement

Takahashi has decided to give one gift to Aoki and one gift to Snuke. There are NN candidates of gifts for Aoki, and their values are A1,A2,,ANA_1, A_2, \ldots,A_N. There are MM candidates of gifts for Snuke, and their values are B1,B2,,BMB_1, B_2, \ldots,B_M.

Takahashi wants to choose gifts so that the difference in values of the two gifts is at most DD.

Determine if he can choose such a pair of gifts. If he can, print the maximum sum of values of the chosen gifts.

Constraints

  • 1N,M2×1051\leq N,M\leq 2\times 10^5
  • 1Ai,Bi10181\leq A_i,B_i\leq 10^{18}
  • 0D10180\leq D \leq 10^{18}
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN MM DD

A1A_1 A2A_2 \ldots ANA_N

B1B_1 B2B_2 \ldots BMB_M

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 1-1.

2 3 2
3 10
2 5 15
8

The difference of values of the two gifts should be at most 22. If he gives a gift with value 33 to Aoki and another with value 55 to Snuke, the condition is satisfied, achieving the maximum possible sum of values. Thus, 3+5=83+5=8 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 3232-bit integer type.

8 6 1
2 5 6 5 2 1 7 9
7 2 5 5 2 4
14