This is my attempt at the question:
http://mirror.codeforces.com/contest/793/submission/27930364
As I couldn't see the test case for which it is failing I was scrolling through other solutions and found 1 that is very similar, but that has passed. Any help with telling me why mine fails?
Similar solution that passes: http://mirror.codeforces.com/contest/793/submission/27902199