Codeforces round 585 div2E help

I tried Reading editorial.I got it is somehow related to Travelling Salesman problem.But i’m not able to simulate the dp states.someone Plzz help me