ランチの組分けのやつプログラミング分からない人には無理かもだけど
PuLPとかのソルバーを使うと100~200行前後でできるよ
https://coin-or.github.io/pulp/CaseStudies/a_set_partitioning_problem.html