October Challenge Remy paints the fence FATCHEF

Hi everyone can you please take a look on my solution and tell me where I’m doing it wrong as i was not able to do it.
Please help…
http://www.codechef.com/viewsolution/5145031

The input is not necessarily sorted and your code assumes that!!
Check out this, I compiled your code on such test case.