I want to find an arrangement to evenly place 12 items ($a_1, a_2, ..., a_{12}$) into 4 boxes ($b_1, b_2, b_3, b_4$) such that the cost is minimal.
Let $b(x)$ be the index of the box that contains item $x$. The cost of an arrangement is
$$\sum_{i=1}^{12} \sum_{j=1}^{12} |b(a_i) - b(a_j)| M_{i,j}$$
where $M$ is a $12\times 12$ constant matrix where all values are non-negatives.
For this instance I could use brute force with $\frac{12!}{3!3!3!3!}$ iterations, but I hope to get a generalised solution that can solve for $N$ items and $K$ boxes in a faster way.
Is there a solution faster than brute force? I have tried some operations research library (such as Google OR tools, CVXPY) but am unable to reduce the problem into a readily acceptable representation.
My intuition tells me there is no faster solution than brute force. In that case, is there an approximation algorithm that can provide good enough solution?