this is a simple math problem.

all coins into the 'right' squares gives a total score of N*A+M*B

as many coins as possible into the 'wrong' squares is 2*min(N,M)*C + (N-min(N,M))*A + (M-min(N,M))*B

follow whichever of the above 2 possibilities gives the highest value