On the optimal folding of protein molecules