subproblem function