Assume we need to start from 0…k-1, there are paths {0…k-1} - {n-1-k…n-1} Each path starts from 0~k-1, calculate the maximum energy starting from a certain starting point for each path, then find the maximum among these paths. For a path, the maximum energy is to find the maximum sum from a starting point to the last value, so we might as well traverse from the last starting point forward and record the maximum sum.
Because the maximum energy is related to previous states, we can also use dynamic programming to solve it: Suppose dp[i] is the maximum energy obtained when reaching the i-th magician, then
dp[i]=max(energy[i],dp[i−k]+energy[i])
Boundary conditions:
dp[i]=energy[i](i<k)
Since the final area has no magicians, we must reach the last k magicians. Therefore, the final maximum is found from the last k dp values.