Compass interview question

Dynamic programming algorithm problem