Cooking Courses

Choose an academy for each of M courses in order, keeping each academy run between S and E long, avoiding one forbidden switch per academy, and paying a switch fee, to minimize total cost.

Medium7Dynamic programmingSliding windowGreedyImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

To earn a cooking certificate you must take MM courses, course 1 through course MM, in that order and exactly once each. The courses are offered by NN academies, and the fee for the same course can differ from academy to academy.

The table below shows example fees for M=5M = 5 and N=4N = 4.

Course 1Course 2Course 3Course 4Course 5
Academy 112138
Academy 212372
Academy 318812
Academy 4101188

You may switch academies partway to cut the cost. Each switch adds a fee of TT. A switch must obey the two rules below.

Rule (a). The number of courses taken consecutively at one academy is at least SS and at most EE. The academy where you take course MM is exempt from the lower bound SS. The upper bound EE still applies there.

Suppose S=2S = 2 and E=3E = 3. If you take course 1 at academy 1, course 2 must also be taken at academy 1, while course 3 may be taken at academy 1 or at another academy. If you take courses 1 through 3 at academy 1, course 4 must be taken at another academy. With S=1S = 1 and E=2E = 2 it is possible to take courses 1 and 2 at academy 3, courses 3 and 4 at academy 1, and course 5 at academy 3 again.

Rule (b). Every academy has exactly one disallowed academy. If the disallowed academy of academy qq is academy pp, you cannot switch from academy pp to academy qq.

In the table below the disallowed academy of academy 1 is academy 2, so switching straight from academy 2 to academy 1 is impossible. Moving through another academy, as in academy 2 → academy 4 → academy 1, is possible.

Disallowed academy
Academy 1Academy 2
Academy 2Academy 3
Academy 3Academy 4
Academy 4Academy 3

Suppose S=2S = 2, E=3E = 3, T=2T = 2, and the two tables above. Reading the academy numbers in course order:

  • 1 → 1 → 1 → 1 → 3 takes four courses in a row at one academy, which breaks rule (a).
  • 2 → 2 → 1 → 1 → 1 switches from academy 2 to academy 1, which breaks rule (b).
  • 3 → 3 → 1 → 1 → 3 is allowed, and the total cost is 1+8+T+1+3+T+2=191 + 8 + T + 1 + 3 + T + 2 = 19.
  • 1 → 1 → 1 → 3 → 3 is allowed, and the total cost is 1+2+1+T+1+2=91 + 2 + 1 + T + 1 + 2 = 9.

Given the fees and the disallowed academies, find the minimum cost of taking all MM courses in order.

Input

The first line contains the number of academies NN, the number of courses MM, the minimum number SS and the maximum number EE of courses that can be taken consecutively at one academy, and the switching fee TT, separated by spaces. (3N30003 \le N \le 3000, 1M30001 \le M \le 3000, N×M3000000N \times M \le 3000000, 1SEM1 \le S \le E \le M, 0T350000 \le T \le 35000)

Each of the next NN lines contains the fees of one academy. The ii-th of these lines contains MM integers, the fees of academy ii for course 1 through course MM, separated by spaces. Each fee is an integer between 11 and 3500035000.

Each of the next NN lines contains the number of the disallowed academy of academy 1 through academy NN, one per line. Each number is between 11 and NN and differs from the number of the academy itself.

Output

Print the minimum cost on the first line.