Critical Cities - CSES Problem

Hi All,
Please help me with the approach for the below problem from the CSES problem set .

https://cses.fi/problemset/task/1703

Thanks in Advance :slight_smile:

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)