Good day!
I am an author of the problem C in the first round of Yandex.Algorithm competition and I would like to tell you about the solution of this problem.
I hope you enjoyed the problem and regret about the weakness of the test-set. This trouble was mentioned by the user maksay, whose quadratic solution is successfully passed system tests. I did not consider this case when created tests for the problem and random tests with deep trees could not challenge this solution.