Min cost to merge stones error


i tried this que but getting error can u plz check approch and tell me where should i optimise
here is my code for this