#18 solved using bottom-up apporach