← Back to Problems

Round-Trip Optimizer

Round-Trip Optimizer

You're routing a delivery driver who must visit every city in a region exactly once and return to the starting depot. Given the pairwise distances between cities, find the minimum total distance of any such round trip.

Input

Line 1: an integer N — the number of cities (including the depot). Next N lines: N space-separated non-negative integers each. The j-th integer on the i-th line is the distance from city i to city j. The matrix is symmetric and the diagonal is zero.

City 0 is the depot — your route starts and ends there.

Output

A single integer: the minimum total distance of a route that starts at city 0, visits every other city exactly once, and returns to city 0.

Example

Input:

4
0 10 15 20
10 0 35 25
15 35 0 30
20 25 30 0

Output:

80

One optimal route is 0 → 1 → 3 → 2 → 0 with cost 10 + 25 + 30 + 15 = 80. Multiple orderings achieve the same total — you only need to report the cost, not the route.

Notes

  • A natural starting point is to enumerate every possible ordering of cities and pick the cheapest — this works comfortably for small inputs.
  • Greedy strategies (e.g., always travel to the nearest unvisited city) are simple to implement and often give reasonable results in practice.

Scoring

Your solution is evaluated against 10 test cases worth 10 points each (100 total). Read the problem carefully and pick your approach.


LEADERBOARD

Rank Developer Score Submitted
1 🥇 shivam
100 / 100
May 22, 2026, 11:31 AM UTC
2 🥈 harshwadhwani-10
100 / 100
May 22, 2026, 11:33 AM UTC
3 🥉 meetpatel-D3sTr0
100 / 100
May 22, 2026, 11:34 AM UTC
4 aniket-axy
100 / 100
May 22, 2026, 11:40 AM UTC
5 parth469
100 / 100
May 22, 2026, 11:45 AM UTC
6 Abhishek0931
100 / 100
May 22, 2026, 11:51 AM UTC
7 Anujjj12
100 / 100
May 22, 2026, 11:52 AM UTC
8 HetaDobariya
100 / 100
May 22, 2026, 11:53 AM UTC
9 gAditya2208
100 / 100
May 22, 2026, 11:55 AM UTC