Hm. Been dragging my feet on these; a month to the day since I last did one. I suck. Anyhow.
I’d been trying to think of a clever way to approach this in the back of my mind for a few weeks, and never bothered trying the brute force approach. My cubic brute force solution was more than sufficient. Full disclosure, I’ve not validated this against the UVA judge, only the book judge, as the UVA queue was busted.