I have tried to implement the solution for http://mirror.codeforces.com/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://mirror.codeforces.com/contest/496/submission/9187611
EDIT: Accepted!!