Optimize problem 5