Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Consider the contribution of each wizard to the answer.
Can you efficiently calculate the total contribution to the answer for all subarrays that end at each index?
Denote the total contribution of all subarrays ending at index i as solve[i]. Can you express solve[i] in terms of solve[m] for some m < i?