Dorsey Thief problem from hacker rank