#C7833. Minimum Operations to Equalize Matrix Elements
Minimum Operations to Equalize Matrix Elements
Minimum Operations to Equalize Matrix Elements
You are given an (n \times m) matrix of integers. In one operation, you can increase or decrease any element of the matrix by 1. Your task is to compute the minimum number of operations required to make all the elements of the matrix equal. The optimal strategy is to make every element equal to the median of all the elements. Mathematically, if the matrix is denoted by (A) and the median is (M), the number of operations is given by:
$$\text{Operations} = \sum_{i=1}^{n}\sum_{j=1}^{m} |A_{ij} - M| $$Read the input from standard input and output the answer to standard output.
inputFormat
The first line contains two integers (n) and (m), representing the number of rows and columns of the matrix, respectively. This is followed by (n) lines, each containing (m) space-separated integers that represent the elements of the matrix.
outputFormat
Output a single integer, which is the minimum number of operations required to make all elements of the matrix equal.## sample
2 2
3 3
2 2
2