Translating the C++ version to Python works very slowly on large-size
input. The one-liner solution looks like a cheat but runs a lot faster.