I did the round #416 as a virtual contest but I didn't quite manage to solve problem C during the time frame. It took me a while but eventually I realized how the O(n^2) DP solution for it works so I thought I'd write a breakdown of the problem for anyone who tried to code it and failed.
The example code is in C++11 but it's mostly just images and text. Hopefully it helps someone!