A selection theorem for optimization problems