Pregunta de entrevista de Bloomberg

Leetcode medium. DP question, brute force + optimization