Codeforces Step 5 problem c doubt

Can anybody help me to solve the codeforces step 5 C problem.

Here is the link
https://codeforces.com/edu/course/2/lesson/2/5/practice/contest/269656/problem/C