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