Time limit for this problem is 0.019s which IMO is not sufficient to solve with Python.
Let me know if this problem is solvable in python.
Thank you.
PS: My code got accepted in Hackerrank(the resource of this problem)
Link: https://www.hackerrank.com/contests/codehurdle/challenges/aliadevil
Edit: TL was increased, now it is solvable.