Bloomberg interview question

Leetcode medium. DP question, brute force + optimization