dp
of size m+1
with all elements set to 0
.op
from m-1
to 0
.op
, iterate over left
from 0
to op
.left
, calculate the score for taking a number from the start (nums[left]
) and from the end (nums[n-1-(op-left)]
), and update dp[left]
with the maximum of these two scores plus the corresponding value from the next operation (dp[left+1]
or dp[left]
).dp[0]
as the final maximum score.