STRANGE-DEVICE codeforces

Link to the problem.
Problem-D
How to solve it?