Codeforces round 585 div2E help

https://codeforces.com/contest/1215/problem/E

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