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 ( CodeChef: Practical coding for everyone ). Can someone explain me what is this code exactly doing. I cannot understand the code. In my submission( CodeChef: Practical coding for everyone ) 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???