Algorithm Uses Dynamic Programming Python