problems 18 and 63, dynamic programming