I have attached the screenshot of the problem statement any help in coming up with the solution.
the link to the problem statement : https://www.hackerrank.com/contests/sears-onsite/challenges/tough-puzzle
http://codeforces.net/predownloaded/dc/3a/dc3a95cd31acc2b686bf505760140774ad84427b.png http://codeforces.net/predownloaded/49/4f/494f83b63edfc3713a93d927c2a7ad4931a0feac.png