#cp problem

unable to cracks its logic…
thanks in advance for help

https://forum.usaco.guide/t/how-to-ask-for-help-on-a-problem/338/2