Slightly optimized Problem 1.