Under construction

Still being built. Things may change

Cat Pot Remodeling Plan (Tentative)

Time limit8sMemory limit512 MB

Summary
Pick male cats for pot A and female cats for pot B, each pot weighing at most W, to minimize the larger of the weight gap and the Cute range.
Level

Hard8 of 10

Topics
Sorting, Two pointers, Dynamic programming
Solved
No attempts yet

Problem

You keep many cats at home. Your daily routine is to photograph your cats napping curled up inside a clay pot, which you call a cat pot. When several cats sleep together in the pot, it is very lovely.

Each cat has a weight mim_i and a Cute value cic_i. Cute is an overall rating of how lovable, innocent, graceful, soft, and fresh a single cat looks on its own. The larger the Cute, the more attractive the cat.

A cat pot also has a value called UnCute, which measures how unbalanced the pot is. Unlike Cute, a smaller UnCute means a better cat pot. The UnCute of a pot is Cmax⁡−Cmin⁡C_{\max} - C_{\min}, where Cmax⁡C_{\max} and Cmin⁡C_{\min} are the largest and smallest Cute among the cats inside. In other words, a good pot holds cats with similar Cute values.

When carrying a cat pot, the total weight of the cats inside, which is the pot weight, must not exceed the limit WW. You want to choose the cats for the pot so that its UnCute is as small as possible. How small can it get?

You once thought solving this problem was enough. Then you found an old clay pot while cleaning the storehouse. Now you have two clay pots at home, so the era of the dual cat pot has begun. The ordinary cat pot is already outdated.

In a dual cat pot, pot AA is for male cats only, and pot BB is for female cats only. The UnCute of a dual cat pot must account for the imbalance in weight between the two pots, not only the imbalance in Cute. Let Mmax⁡M_{\max} be the weight of the heavier pot and Mmin⁡M_{\min} the weight of the lighter pot (if both pots weigh the same, Mmax⁡=Mmin⁡M_{\max} = M_{\min}). The UnCute of a dual cat pot is

max⁡{Mmax⁡−Mmin⁡,Cmax⁡−Cmin⁡}\max\{M_{\max} - M_{\min}, C_{\max} - C_{\min}\}

Here Cmax⁡C_{\max} and Cmin⁡C_{\min} are the largest and smallest Cute values among the cats in pot AA or pot BB.

Since the pots are carried around, Mmax⁡M_{\max} must not exceed the limit WW. Choose the cats for the dual cat pot so that its UnCute is as small as possible. How small can it get?

Input

The first line contains three integers NAN_A (1≤NA≤5001 \le N_A \le 500), NBN_B (1≤NB≤5001 \le N_B \le 500), and WW (1≤W≤10,0001 \le W \le 10{,}000), separated by spaces. NAN_A is the number of male cats, NBN_B is the number of female cats, and WW is the weight limit of a pot.

The next NAN_A lines each contain mA,im_{A,i} (1≤mA,i≤10,0001 \le m_{A,i} \le 10{,}000) and cA,ic_{A,i} (1≤cA,i≤1091 \le c_{A,i} \le 10^9), the weight and Cute of the ii-th male cat.

The next NBN_B lines each contain mB,im_{B,i} (1≤mB,i≤10,0001 \le m_{B,i} \le 10{,}000) and cB,ic_{B,i} (1≤cB,i≤1091 \le c_{B,i} \le 10^9), the weight and Cute of the ii-th female cat.

At least one male cat and at least one female cat have weight at most WW.

NA NB W
mA,1 cA,1
...
mA,NA cA,NA
mB,1 cB,1
...
mB,NB cB,NB

Output

Print the minimum possible UnCute of the dual cat pot, under the condition that Mmax⁡M_{\max} does not exceed WW. Each pot must contain at least one cat.

Examples3

  1. Example 1

    Input
    4 3 12
    3 6
    2 4
    7 9
    10 1
    6 5
    8 4
    15 19
    
    Expected output
    2
    
  2. Example 2

    Input
    1 3 10
    1 15
    6 8
    5 9
    8 7
    
    Expected output
    6
    
  3. Example 3

    Input
    8 6 65
    30 98
    27 51
    4 74
    65 87
    49 19
    27 48
    43 7
    35 28
    43 69
    8 47
    64 75
    18 23
    54 29
    40 43
    
    Expected output
    8