my solution is exact same as yours but that is giving only 30 points after submition. please check my solution. https://www.codechef.com/viewsolution/10207113
You have to update prev correctly. I have edited (only one small change) your code, check it here-
https://www.codechef.com/viewsolution/10252513