Source: https://cemc.uwaterloo.ca/contests/past_contests/2019/2019BCCContestSolutions9_10.pdf