P8983 "DROI" Round 1 Out of Control

Background

Being out of control might actually be being rational.

Description

You are given an $n \times m$ matrix $G$ and two permutations $p, q$ of length $m$. We say an element $G_{i,j}$ is **out of control** if and only if $\vert G_{i,j} - G_{i-1,p_j} \vert > C$ **and** $\vert G_{i,j} - G_{i+1,q_j} \vert > C$, where $C$ is a given constant. In particular, we define that no element in row $1$ and row $n$ is out of control no matter what. Then you are also given two sequences $A$ and $B$, both of length $k$. You have $k$ types of operations. The $i$-th operation adds $A_i$ to all elements in a chosen row, and costs $B_i$. **Each operation type can be used any number of times, but for each row, you may either apply exactly one of these operation types or do nothing. Also, you must ensure that among any two adjacent rows, at most one row is operated on.** Find the minimum total cost to make every element in matrix $G$ not out of control (the testdata guarantees that a solution exists).

Input Format

The first line contains four integers $n, m, k, C$. The next $n + 4$ lines are as follows: the first $n$ lines each contain $m$ numbers, describing matrix $G$. Lines $n + 1$ and $n + 2$ each contain $m$ numbers, describing permutations $p$ and $q$, respectively. The last two lines each contain $k$ numbers, describing sequences $A$ and $B$, respectively.

Output Format

Output one integer in a single line, representing the answer.

Explanation/Hint

#### Sample Explanation #1 Obviously, for sample 1, performing no operation is enough to ensure that all elements are not out of control. ------------ #### Sample Explanation #2 Apply operation $3$ on row $3$, and apply operation $4$ on row $7$. It can be proven that there is no better solution. ------------ #### Constraints **"This problem uses bundled subtasks."** - $\operatorname{Subtask} 1(10\%)$: $n, m, k \leq 8$. - $\operatorname{Subtask} 2(30\%)$: $m \leq 50, k \leq 100$. - $\operatorname{Subtask} 3(20\%)$: $m \leq 50, k \leq 1000$. - $\operatorname{Subtask} 4(40\%)$: no special restrictions. For $100\%$ of the testdata: $3 \leq n \leq 50$, $1 \leq m \leq 300$, $0 \leq k \leq 2000$, $C, G_{i,j}, A_i, B_i \leq 10^6$. **The input size is large. Please use a fast input method.** ------------ #### Hint - This problem is not strict about constant factors. If you feel your algorithm is just a bit too slow to pass the next Subtask, please do not get stuck optimizing meaningless constants, because that missing part may need a better algorithm to make up for it. Translated by ChatGPT 5