Exam in BerSU (hard version)

CodeForces - 1185C2