直接暴力解...
不會寫再看(注意變數名很亂):https://github.com/ChuangRoy/RoyChuangs-Code/blob/master/ZeroJudge/f439.py
用C++重寫 (想不到再看)
https://raw.githubusercontent.com/ChuangRoy/RoyChuangs-Code/refs/heads/master/C_CPP/ZeroJudge/f439.cpp