Let = maximum score to reach index . The base case is . The transition: .
You take the best score from any reachable previous position and add the current element. Naive implementation: for each , loop through the last positions. That's time. With and , you're looking at operations. Time to improve.