On a constrained variational problem with an arbitrary number of free boundaries