Sum of Good Subsequences
Track, for each value, how many good subsequences end with it and their total sum, so each new element extends only its neighbours in value.
Track, for each value, how many good subsequences end with it and their total sum, so each new element extends only its neighbours in value.