solved with DP+DFS