#K52652. Minimum Operations to Equalize Grid

    ID: 29357 Type: Default 1000ms 256MiB

Minimum Operations to Equalize Grid

Minimum Operations to Equalize Grid

You are given a grid of integers with n rows and m columns. In one operation, you can increase the value of any one cell by 1.

The goal is to make all the cells in the grid have the same value by performing a sequence of operations. The optimal strategy is to increase every cell to the maximum value present in the grid. Mathematically, if \(g_{ij}\) represents the value at row \(i\) and column \(j\), and \(M = \max_{1 \le i \le n, 1 \le j \le m} g_{ij}\), the minimum number of operations required is given by:

[ \text{Operations} = \sum_{i=1}^{n}\sum_{j=1}^{m} (M - g_{ij}) ]

Your task is to compute and print this number.

inputFormat

The first line contains two integers (n) and (m) representing the number of rows and columns, respectively. (n) lines follow, each containing (m) space-separated integers representing the grid values.

outputFormat

Output a single integer that represents the minimum number of operations required to make all grid cells equal.## sample

3 3
1 2 3
4 5 6
7 8 9
36