v1. works for basic cases, but Time Limit Exceeded