> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Total Importance of Roads

> Tested Python solution for LeetCode 2285 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 2285, [Medium](/catalog/medium). Topics: [Greedy](/catalog/topics/greedy), [Graph Theory](/catalog/topics/graph-theory), [Sorting](/catalog/topics/sorting), [Heap (Priority Queue)](/catalog/topics/heap-priority-queue). [View on LeetCode](https://leetcode.com/problems/maximum-total-importance-of-roads/description/).

Generate this problem as a practice environment: tested reference solution, 19 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2285   # by problem number
lcpy gen -s maximum_total_importance_of_roads   # by problem name
```

## Problem

You are given an integer `n` denoting the number of cities in a country. The cities are numbered from `0` to `n - 1`.

You are also given a 2D integer array `roads` where `roads[i] = [a<sub>i</sub>, b<sub>i</sub>]` denotes that there exists a **bidirectional** road connecting cities `a<sub>i</sub>` and `b<sub>i</sub>`.

You need to assign each city with an integer value from `1` to `n`, where each value can only be used **once**. The **importance** of a road is then defined as the **sum** of the values of the two cities it connects.

Return *the **maximum total importance** of all roads possible after assigning the values optimally.*

### Examples

![Example 1](https://assets.leetcode.com/uploads/2022/04/07/ex1drawio.png)

```
Input: n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]
Output: 43
Explanation: The assigned values are [2,4,5,3,1].
The total importance of all roads is 6 + 9 + 8 + 7 + 7 + 6 = 43.
```

![Example 2](https://assets.leetcode.com/uploads/2022/04/07/ex2drawio.png)

```
Input: n = 5, roads = [[0,3],[2,4],[1,3]]
Output: 20
Explanation: The assigned values are [4,3,2,5,1].
The total importance of all roads is 9 + 3 + 8 = 20.
```

### Constraints

* 2 \<= n \<= 5 \* 10\<sup>4\</sup>
* 1 \<= roads.length \<= 5 \* 10\<sup>4\</sup>
* roads\[i].length == 2
* 0 \<= a\<sub>i\</sub>, b\<sub>i\</sub> \<= n - 1
* a\<sub>i\</sub> != b\<sub>i\</sub>
* There are no duplicate roads.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_total_importance_of_roads/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_total_importance_of_roads/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(E + V log V) where E = len(roads), V = n
    # Space: O(V)
    def maximum_importance(self, n: int, roads: list[list[int]]) -> int:
        degree = [0] * n
        for a, b in roads:
            degree[a] += 1
            degree[b] += 1
        degree.sort()
        return sum(d * (i + 1) for i, d in enumerate(degree))
```

## Complexity

| Time | Space |
| - | - |
| O(E + V log V) where E = len(roads), V = n | O(V) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.