Please Explain C from Codeforces Round 794 div 2

I was able to solve A & B in this contest but not able to solve C

If anyone solved this please explain it to me :slight_smile:
Thank you in advance