Time limit
2s
Memory limit
128 MB
You are given an N×M array of integers. You may pair two cells that share an edge. Each cell can be used in at most one pair, and some cells may remain unpaired.
The value of a pair is the absolute difference between the two numbers. If this difference is greater than T, the pair is not allowed. Choose allowed pairs to maximize the total value.
The first line contains N, M (1 ≤ N, M ≤ 30) and T (0 ≤ T ≤ 10,000,000). Each of the next N lines contains M integers describing one row of the array. The absolute value of every array element is at most 1,000,000.
Print the maximum possible total value of all chosen pairs.