naive solution (using memoization) accepted