Problem 25 optimization