Problem 5 optimization