ABCPATH: dfs with memoization, can't believe it was too slow
ACMAKER: dp