P12971 [CCO 2025] Shopping Deals
Description
You are shopping from a store that sells a total of $M$ items. The store layout can be modelled as a two-dimensional plane, where the $i$-th item is located at the point $(x_i, y_i)$ and has a price of $p_i$.
The store offers $N$ shopping deals. The $i$-th shopping deal is specified by a point $(a_i, b_i)$, and for a cost of $c_i$, you can obtain one of every item within exactly one of the following four regions of your choice:
- The region of points $(x, y)$ such that $x \leq a_i$ and $y \leq b_i$.
- The region of points $(x, y)$ such that $x \leq a_i$ and $y \geq b_i$.
- The region of points $(x, y)$ such that $x \geq a_i$ and $y \leq b_i$.
- The region of points $(x, y)$ such that $x \geq a_i$ and $y \geq b_i$.
Each shopping deal can only be used at most once. Items can also be purchased individually by paying their respective price $p_i$.
You want to obtain at least one of each item in the store. Find the minimum total cost you must pay to do so.
Input Format
The first line of input contains two space-separated integers $N$ and $M$.
The next $N$ lines of input each contain three space-separated integers, $a_i$, $b_i$, and $c_i$ ($-10^9 \leq a_i, b_i \leq 10^9$, $1 \leq c_i \leq 10^9$).
The next $M$ lines of input each contain three space-separated integers, $x_i$, $y_i$, and $p_i$ ($-10^9 \leq x_i, y_i \leq 10^9$, $1 \leq p_i \leq 10^9$).
Output Format
On a single line, output the minimum total cost that you must pay to obtain at least one of each item.
Explanation/Hint
**Sample 1 Explanation**
Use the first shopping deal on the region $\{(x, y) \mid x \leq 1, y \geq 1\}$ to obtain the second item. Then, purchase items 1, 3, and 4 individually. The total cost is $3 + (2 + 4 + 3) = 12$.
The following table shows how the available $25$ marks are distributed:
| Marks Awarded | Bounds on $N$ | Bounds on $M$ | Additional Constraints |
| :---: | :---: | :---: | :---: |
| 1 mark | $1 \leq N \leq 8$ | $1 \leq M \leq 20$ | None |
| 3 marks | $1 \leq N \leq 70$ | $1 \leq M \leq 20$ | None |
| 3 marks | $1 \leq N \leq 70$ | $1 \leq M \leq 70$ | None |
| 4 marks | $1 < N \leq 100$ | $1 < M \leq 100000$ | No two points $(a_i, b_i)$ or $(x_j, y_j)$ have the same $x$ or $y$-coordinate. |
| 2 marks | $1 \leq N \leq 100$ | $1 \leq M \leq 100000$ | None |
| 8 marks | $1 \leq N \leq 1000$ | $1 \leq M \leq 100000$ | No two points $(a_i, b_i)$ or $(x_j, y_j)$ have the same $x$ or $y$-coordinate. |
| 4 marks | $1 \leq N \leq 1000$ | $1 \leq M \leq 100000$ | None |