Source: https://www.cemc.uwaterloo.ca/contests/past_contests/2022/2022BCCContestSolutions5_6.pdf