Dynamic Programming Python