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

#FW0000084. Đường mới tại Berland (Roads in Berland)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đường mới tại Berland (Roads in Berland)

Roads in Berland

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given a connected undirected network represented by its current shortest-distance matrix, sequentially add qq roads (a,b,w)(a,b,w). After each addition print the sum of shortest distances across unordered pairs i<ji<j. Non-improving edges leave the sum unchanged.

Input

First nn, a symmetric all-pairs shortest-distance matrix with zero diagonal, then qq new roads abwa b w (1-based).

Output

Print qq space-separated integer sums after consecutive road additions.

Subtasks

  • Subtask 1 (40 points): 2≤n≤7, 1≤q≤62\le n\le 7,\ 1\le q\le6. Aii=0A_{ii}=0, ma trận đối xứng và đã là ma trận đường đi ngắn nhất; 1≤w≤1061\le w\le10^6.

  • Subtask 2 (60 points): 2≤n≤30, 1≤q≤122\le n\le 30,\ 1\le q\le12. Aii=0A_{ii}=0, ma trận đối xứng và đã là ma trận đường đi ngắn nhất; 1≤w≤1061\le w\le10^6.

Examples

Input

3
0 2 5
2 0 3
5 3 0
2
1 3 2
1 2 8

Output

7 7

Explanation

The initial unordered-pair sum is 10. Adding edge 1–3 of length 2 reduces it to 7; the second edge of length 8 does not improve the network.