Optimized and simplified problem 26.