Here is the link: RoadOrFlightHard I got the solution but it takes dp[400000][40][2] which is very huge as far as memory is concerned. In the editorial they have added a line that city(i) calculation depends on city(i+1). I was not able to get that observation and I am not getting how to implement it. I saw one code but still did not understand. Thank You.