Dots.........

We have a rectangle subdivided into squares. In each square there are
some of dots.

By clicking on a line which separates two squares, we make this line
disappear, merging the two squares into a rectangle. Then, we can merge
rectangles into bigger rectangles, and so on. We play until all lines
disappear, and we obtain a rectangle without lines, filled with dots.

Sometimes merging two rectangles is rewarded. Let d₁ and d₂ be the
numbers of dots in the two merged rectangles. It is worth it to merge
rectangles which have a similar number of dots: if |d₁ − d₂| ≤ M, we get
K bonus dots. Additionally, if d₁ + d₂ is divisible by K, we get N bonus
dots. All bonus dots appear in the merged square (instead of the line
which was removed).

The goal of the game is to obtain as many dots in total as possible.

Input

The first line contains five numbers: X, Y, K, N, M. We have 1 ≤ X ≤ 15,
1 ≤ Y ≤ 15, 2 ≤ K ≤ 10, 0 ≤ N ≤ 1000000, 0 ≤ M ≤ 1000003.

The following Y lines describe the amount of dots in each square. i − th
of these lines contains X numbers a_(i, 1), a_(i, 2), …, a_(i, X), where
a_(i, j) is the number of dots in the square in row i, column j
(1 ≤ a_(i, j) ≤ 1000000).

Output

Output the highest number of dots that can be obtained in the end.

Problem information

Author: Eryk Kopczynski

Generation: 2026-01-25T21:42:56.702Z

© Jutge.org, 2006–2026.
https://jutge.org
