Frog fell into a maze. This maze is a rectangle containing $N$ rows and $M$ columns. Each grid in this maze contains a number, which is called the magic value. Frog now stays at grid (1, 1), and he wants to go to grid (N, M). For each step, he can go to either the grid right to his current location or the grid below his location. Formally, he can move from grid (x, y) to (x + 1, y) or (x, y +1), if the grid he wants to go exists.
Frog is a perfectionist, so he'd like to find the most beautiful path. He defines the beauty of a path in the following way. Let’s denote the magic values along a path from (1, 1) to (n, m) as $A_1, A_2, … A_{N+M-1}$, and $A_{avg}$ is the average value of all $A_i$. The beauty of the path is $(N + M – 1)$ multiplies the variance of the values:$(N+M-1)\sum_{i=1}^{N+M-1}(A_{i}-A_{avg})^{2}$
In Frog's opinion, the smaller, the better. A path with smaller beauty value is more beautiful. He asks you to help him find the most beautiful path.