Editorial
View a valid tour as two increasing paths from vertex to vertex . They share only their endpoints.
Let be the minimum total weight of two such paths after assigning vertices through , with endpoints and (). Initially, . When adding , if , append after , giving . For , append after the other path's endpoint , giving . Missing edges cannot be used.
After processing every vertex, connect endpoints and . The minimum of is the answer. Store the previous endpoint chosen for each to trace the selected edges backward and reconstruct both paths. If no tour exists, print . Edge weight is valid, so use to identify a missing edge. A tour can cost up to , requiring 64-bit integers.
The time and space complexities are both . For subtask , one may enumerate every assignment of intermediate vertices to the two paths.
Solution written by GPT6