Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Can you solve each query in <code>O(nums.length)</code> with dynamic programming?
In order to optimize, we will use segment tree where each node contains the maximum value of (front element has been chosen or not, back element has been chosen or not).