solving dfs and dp