Goldman Sachs interview question

Leetcode Hard question, similar to get the maximum score