Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
At first glance, the solution seems to be greedy, but if you try to greedily take the largest value from the beginning or the end, this will not be optimal.
You should try all scenarios but this will be costly.
Memoizing the pre-visited states while trying all the possible scenarios will reduce the complexity, and hence dp is a perfect choice here.