PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000085. Greg và đồ thị (Greg and Graph)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Greg và đồ thị (Greg and Graph)

Greg and Graph

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given a directed complete nonnegative weighted graph and an order for deleting all vertices, output the sum of shortest-path distances over all ordered pairs of currently remaining vertices immediately before each deletion. Deleted vertices cannot be used as intermediates.

Input

First nn, then an n×nn\times n directed cost matrix with zero diagonal, followed by a permutation giving the deletion order (1-based).

Output

Print nn integer sums in the original deletion order, separated by spaces.

Subtasks

  • Subtask 1 (40 points): 2≤n≤72\le n\le7. Aii=0A_{ii}=0, 1≤Aij≤1061\le A_{ij}\le10^6 nếu i≠ji\ne j; ma trận có hướng và mọi đỉnh đi tới nhau.

  • Subtask 2 (60 points): 2≤n≤302\le n\le30. Aii=0A_{ii}=0, 1≤Aij≤1061\le A_{ij}\le10^6 nếu i≠ji\ne j; ma trận có hướng và mọi đỉnh đi tới nhau.

Examples

Input

3
0 2 7
2 0 3
7 3 0
3 2 1

Output

20 4 0

Explanation

Before deleting vertex 3, the total shortest-distance sum across ordered pairs is 20. With vertices 1 and 2 remaining it becomes 4, and with one vertex it is 0.