Cat Pot Remodeling Plan (Tentative)
Time limit8sMemory limit512 MB
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 and a Cute value . 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 , where and 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 . 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 is for male cats only, and pot 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 be the weight of the heavier pot and the weight of the lighter pot (if both pots weigh the same, ). The UnCute of a dual cat pot is
Here and are the largest and smallest Cute values among the cats in pot or pot .
Since the pots are carried around, must not exceed the limit . 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 (), (), and (), separated by spaces. is the number of male cats, is the number of female cats, and is the weight limit of a pot.
The next lines each contain () and (), the weight and Cute of the -th male cat.
The next lines each contain () and (), the weight and Cute of the -th female cat.
At least one male cat and at least one female cat have weight at most .
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 does not exceed . Each pot must contain at least one cat.