A variational problem for manifold valued functions