Solving the problem using DP.Bottom-up strategy