added recursive dp solution