#C11855. Minimum Steps to Transform Matrix

    ID: 41217 Type: Default 1000ms 256MiB

Minimum Steps to Transform Matrix

Minimum Steps to Transform Matrix

You are given an integer matrix A of size n × m and a target integer T. In one step, you can increment or decrement any element of the matrix by 1. Your task is to determine the minimum number of steps required so that every element in the matrix becomes equal to T.

The answer can be formulated as:

$$ \text{steps} = \sum_{i=1}^{n} \sum_{j=1}^{m} |A_{ij} - T| $$

where \(A_{ij}\) represents the element at the ith row and jth column of the matrix.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains two space-separated integers n and m, representing the number of rows and columns of the matrix, respectively.
  • The following n lines each contain m space-separated integers representing the elements of the matrix.
  • The last line contains a single integer T which is the target value.

outputFormat

Print to stdout a single integer which is the minimum number of steps required to make every element of the matrix equal to T.

## sample
2 3
1 2 3
4 5 6
3
9