horrible spoj !!

Was trying to solve the spoj http://www.spoj.com/problems/HORRIBLE/ .My solution is here http://ideone.com/HEmQXl. but still the WA is coming plzz help guys…:slight_smile: