Can someone explain me this code for RGAME?

For the question rupsa and the game the most common solution that I found in submissions is this one ( https://www.codechef.com/viewsolution/15179166 ). Can someone explain me what is this code exactly doing. I cannot understand the code. In my submission( https://www.codechef.com/viewsolution/15354831 ) I imitated the same situation virtually i.e. by placing the numbers one by one in an array to find sum of all gameplays. I also want to ask that is this the correct method to solve any programming problem i.e. by imitating it exactly in the code???

//