2285. Maximum Total Importance of Roads

class Solution:
    def maximumImportance(self, n: int, roads: List[List[int]]) -> int:
        # Find connections : sort with connection numbers
        # Assign accordingly

        d = defaultdict(int)
        ans = 0

        for (a, b) in roads:
            d[a] += 1
            d[b] += 1

        sortedValues = sorted(d.values(), key= lambda x : -1 * x)

        ans = 0
        val = n
        for e in sortedValues:
            ans += (e * val)  
            val -= 1

        return ans