I’m sorry but I myself was unable to solve subtask 2 and 3 of this question. You should take a look at the EDITORIAL