Just a piece of advice:
Read well the problem statements.
During Codeforces Round 932 (Div. 2), I was trying to solve a problem that was completely different from what the problem 1935D - Экзамен в ЦПМ was. I thought x and y had to be in the given set, and had no idea what magic to do to make it not at least n^2 (maybe that should have given me a hint that I was misreading).
Another piece of advice:
Go through the example test cases
If I had gone through the example I would have noticed.
(this is mostly a note to myself)