Hello I am new to competitive programming. I am solving ques number 2 of the online round of the following link but not getting any idea of approach

I am solving ques number 2 of the online round of the following link but not getting any idea of approach
Anyone please tell me the approach how can i solve it or any similar questions
Click here for the link
Thanks in advance

At each state you have 2 options for a pen(except 1st, where you can select any of the three). Since you cannot select the pen the previous person selected, you also need the penid of the previous state.

Rest is a simple minimization of all the two states.

Absolutely correct but i m not getting approach for this question.Means how to write the code for this? Thanx @baap_hu_mae

Practice recursion and then Dynamic Programming.