Optimized problem 5 runtime.