Solution for http://www.codechef.com/status/UASEQ.