Yet Another MAD TEAM

2 secs 1024 MB
tatyam's icon tatyam

https://atcoder.jp/contests/zone2021/editorial/1197 の DP 解で線形です。

他にも解法はたくさん