actually doing dp and added memoization func