Yandex interview question

Very hard algorithm contest at yandex