#P6932. Maximize Widget Market Profit
Maximize Widget Market Profit
Maximize Widget Market Profit
You are a middleman in the widget market. Your task is to choose a widget producer company and a widget consumer company in order to maximize your profit. Each widget consumer company offers to buy one widget per day until a certain end date, at a given buying price. Each widget producer company can start delivering widgets from a given start date, at a given selling price. Once you sign a contract with a producer and a consumer, you will buy one widget per day starting on the producer's start date and ending on the consumer's end date. On each day, your profit is the difference between the consumer's buying price and the producer's selling price. If the producer's start date is later than the consumer's end date, no widgets are delivered and the profit is 0.
Your goal is to select one producer and one consumer such that the profit, computed as
\(\text{Profit} = (\text{consumer buying price} - \text{producer selling price}) \times (\text{consumer end date} - \text{producer start date} + 1)\)
is maximized. Note that you must sign exactly one contract with a producer and one with a consumer. Even if the computed profit is negative for a valid pairing, you may choose a pairing that results in 0 profit by selecting a consumer whose end date is before the producer's start date.
inputFormat
The first line contains two integers n and m separated by a space where n is the number of widget producer companies and m is the number of widget consumer companies.
The next n lines each contain two integers si and pi, where si is the start date on which the i-th producer can begin delivering widgets and pi is its selling price per widget.
The next m lines each contain two integers ej and bj, where ej is the end date until which the j-th consumer will buy widgets and bj is its buying price per widget.
outputFormat
Output a single integer representing the maximum profit achievable.
sample
1 1
5 3
7 8
15