Hackerearth contest - Scoring stores

I was trying to solve the problem Scoring Stores.
Link: Scoring stores | Practice Problems

The tags state that is based on 2 dimensional dynamic programming.
I was thinking something along using a maxheap to keep track of the max every time and then update the next element. The editorial simply gives the code but does not explain the logic.
Help would be really appreciated.