def calculate_shortest_paths_sum(n: int, matrix: List[List[int]], x: List[int]) -> List[int]:
    """
    Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game:  The game consists of n steps.  On the i-th step Greg removes vertex number x_{i} from the graph. As Greg removes a vertex, he also removes all the edges that go in and out of this vertex.  Before executing each step, Greg wants to know the sum of lengths of the shortest paths between all pairs of the remaining vertices. The shortest path can go through any remaining vertex. In other words, if we assume that d(i, v, u) is the shortest path between vertices v and u in the graph that formed before deleting vertex x_{i}, then Greg wants to know the value of the following sum: $\sum_{v, u, v \neq u} d(i, v, u)$.
    
    Help Greg, print the value of the required sum before each step.
    
    
    -----Input-----
    
    The first line contains integer n (1 ≤ n ≤ 500) — the number of vertices in the graph.
    
    Next n lines contain n integers each — the graph adjacency matrix: the j-th number in the i-th line a_{ij} (1 ≤ a_{ij} ≤ 10^5, a_{ii} = 0) represents the weight of the edge that goes from vertex i to vertex j.
    
    The next line contains n distinct integers: x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ n) — the vertices that Greg deletes.
    
    
    -----Output-----
    
    Print n integers — the i-th number equals the required sum before the i-th step.
    
    Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
    
    
    -----Examples-----
    Input
    1
    0
    1
    
    Output
    0
    Input
    2
    0 5
    4 0
    1 2
    
    Output
    9 0
    Input
    4
    0 3 1 1
    6 0 400 1
    2 4 0 1
    1 1 1 0
    4 1 2 3
    
    Output
    17 23 404 0
    """


# Tests

def check(candidate):
    assert candidate(1, [[0]], [1]) == [0]
    assert candidate(2, [[0, 5], [4, 0]], [1, 2]) == [9, 0]
    assert candidate(4, [[0, 3, 1, 1], [6, 0, 400, 1], [2, 4, 0, 1], [1, 1, 1, 0]], [4, 1, 2, 3]) == [17, 23, 404, 0]
    assert candidate(4, [[0, 57148, 51001, 13357], [71125, 0, 98369, 67226], [49388, 90852, 0, 66291], [39573, 38165, 97007, 0]], [2, 3, 1, 4]) == [723897, 306638, 52930, 0]
    assert candidate(5, [[0, 27799, 15529, 16434, 44291], [47134, 0, 90227, 26873, 52252], [41605, 21269, 0, 9135, 55784], [70744, 17563, 79061, 0, 73981], [70529, 35681, 91073, 52031, 0]], [5, 2, 3, 1, 4]) == [896203, 429762, 232508, 87178, 0]
    assert candidate(6, [[0, 72137, 71041, 29217, 96749, 46417], [40199, 0, 55907, 57677, 68590, 78796], [83463, 50721, 0, 30963, 31779, 28646], [94529, 47831, 98222, 0, 61665, 73941], [24397, 66286, 2971, 81613, 0, 52501], [26285, 3381, 51438, 45360, 20160, 0]], [6, 3, 2, 4, 5, 1]) == [1321441, 1030477, 698557, 345837, 121146, 0]
    assert candidate(7, [[0, 34385, 31901, 51111, 10191, 14089, 95685], [11396, 0, 8701, 33277, 1481, 517, 46253], [51313, 2255, 0, 5948, 66085, 37201, 65310], [21105, 60985, 10748, 0, 89271, 42883, 77345], [34686, 29401, 73565, 47795, 0, 13793, 66997], [70279, 49576, 62900, 40002, 70943, 0, 89601], [65045, 1681, 28239, 12023, 40414, 89585, 0]], [3, 5, 7, 6, 1, 2, 4]) == [1108867, 1016339, 729930, 407114, 206764, 94262, 0]
    assert candidate(8, [[0, 74961, 47889, 4733, 72876, 21399, 63105, 48239], [15623, 0, 9680, 89133, 57989, 63401, 26001, 29608], [42369, 82390, 0, 32866, 46171, 11871, 67489, 54070], [23425, 80027, 18270, 0, 28105, 42657, 40876, 29267], [78793, 18701, 7655, 94798, 0, 88885, 71424, 86914], [44835, 76636, 11553, 46031, 13617, 0, 16971, 51915], [33037, 53719, 43116, 52806, 56897, 71241, 0, 11629], [2119, 62373, 93265, 69513, 5770, 90751, 36619, 0]], [3, 7, 6, 5, 8, 1, 2, 4]) == [1450303, 1188349, 900316, 531281, 383344, 219125, 169160, 0]
    assert candidate(9, [[0, 85236, 27579, 82251, 69479, 24737, 87917, 15149, 52311], [59640, 0, 74687, 34711, 3685, 30121, 4961, 7552, 83399], [33376, 68733, 0, 81357, 18042, 74297, 15466, 29476, 5865], [7493, 5601, 3321, 0, 20263, 55901, 45756, 55361, 87633], [26751, 17161, 76681, 40376, 0, 39745, 50717, 56887, 90055], [18885, 76353, 47089, 43601, 21561, 0, 60571, 33551, 53753], [74595, 877, 71853, 93156, 97499, 70876, 0, 22713, 63961], [67725, 25309, 56358, 92376, 40641, 35433, 39781, 0, 97482], [81818, 12561, 85961, 81445, 3941, 76799, 31701, 43725, 0]], [6, 2, 9, 3, 5, 7, 1, 4, 8]) == [2106523, 1533575, 1645151, 1255230, 946667, 618567, 287636, 147737, 0]
check(calculate_shortest_paths_sum)