Problem codechef cook off chef and paporini

Provide a simple soln which doesn’t use dp fro this problem where we store the indices as a pair and then minimize plz provide code and explain even if it shows tle it should work for smaller testcases I want to know concept .
Link is https://www.codechef.com/COOK115B/problems/PEPPERA

plz reply to this doubt