Problem 2263 · Hard
Run a DP over the original values as targets, once for each orientation, and keep the cheaper cost.
Open the interactive visualisation