UVa 10970 - Big Chocolate

10970 - Big Chocolate
http://acm.uva.es/p/v109/10970.html

Summary
A chocolate bar is a MxN rectangle, composed of unit squares. Determine, how many straight-line cuts are necessary to split it into unit-square pieces. Each cut can only divide one connected piece of chocolate into two.

Explanation
A straightforward solution is to use dynamic programming. If $$f(m,n)$$ denotes the minimum number of cuts for MxN chocolate bar, then:


 * $$f(1,n) = n-1$$
 * $$f(m,n) = \min_{1 \le k \le m-1} [1 + f(k,n) + f(m-k,n)]$$ (for $$m > 1$$).

It can be noticed, that the function $$f(m,n)=mn-1$$ is the solution to above relations.

Input
2 2 1 1 1 3 5 5 5 10 10 5

Output
3 0 4 24 49 49