I have tried to implement the solution for http://codeforces.net/problemset/problem/496/E based on what I could understand by translating the editorial to english. My code is failing on 10th test case. Please help me find a bug in my code. Thanks in advance. :)
http://codeforces.net/contest/496/submission/9187611
EDIT: Accepted!!