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

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

2 lộ trình

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 nn candidate gifts for Aoki with values a1,…,ana_1,\ldots,a_n, and mm candidate gifts for Snuke with values b1,…,bmb_1,\ldots,b_m.

He wants the values of the chosen gifts to differ by at most DD. 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 n,m,Dn,m,D, the numbers of candidate gifts and the maximum allowed difference.
  • The second line contains nn integers a1,…,ana_1,\ldots,a_n, the gift values for Aoki.
  • The third line contains mm integers b1,…,bmb_1,\ldots,b_m, the gift values for Snuke.

Output

Print the maximum sum of a valid pair; print -1 if none exists.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤81\le n,m\le 8; 0≤D≤10180\le D\le10^{18}; 1≤ai,bj≤10181\le a_i,b_j\le10^{18} for every element.
  • Subtask 2 (30 points): 1≤n,m≤17001\le n,m\le 1700; 0≤D≤10180\le D\le10^{18}; 1≤ai,bj≤10181\le a_i,b_j\le10^{18} for every element.
  • Subtask 3 (50 points): 1≤n,m≤2000001\le n,m\le 200000; 0≤D≤10180\le D\le10^{18}; 1≤ai,bj≤10181\le a_i,b_j\le10^{18} for every element.

Examples

Input

2 3 2
3 10
2 5 15

Output

8

Explanation

If Aoki receives the gift valued 33, Snuke may receive a gift valued 22 or 55, yielding sums 55 or 88; both differences are at most 22. The gift valued 1010 has no compatible counterpart among [2,5,15][2,5,15]. Therefore the maximum is 3+5=83+5=8.