#P9153. 「SvR-2」1+2=3(加强版)

「SvR-2」1+2=3(加强版)

Problem Description

You have some wooden sticks. Each stick has a number on the left and a number on the right. The numbers are natural numbers in [0,c)[0,c). You need to connect all sticks together so that the number of adjacent pairs whose sum is cc is as large as possible.

For example, when c=3c=3, there are two sticks: 1 - 21\text{ - }2 and 1 - 01\text{ - }0. If you connect them as 1 - 0,1 - 21\text{ - }0,1\text{ - }2, then the number of adjacent pairs whose sum is 33 is 00. But if you connect them as $1\text{ - }\textcolor{red}{\underline{\textbf 2}},\textcolor{red}{\underline{\textbf 1}}\text{ - }0$, then the number of adjacent pairs whose sum is 33 is 11, because 2+1=32+1=3.

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, the number of test cases.

For each test case, the first line contains a positive integer cc. Then follow cc lines, each containing cc integers. The jj-th integer in the ii-th line, ai,ja_{i,j}, is the number of sticks of type (i1) - (j1)(i-1)\text{ - }(j-1).

Output Format

Output TT lines. Each line contains one integer, the answer.

1
3
4 1 3
4 7 7
9 10 3

31

Hint

For 100%100\% of the testdata, 1T1051\le T\le10^5, 1ai,j1091\le a_{i,j}\le10^9, 3c1033\le c\le10^3, c25×106\sum c^2\le5\times10^6.

Note: The testdata for this problem is relatively weak. If you have any hacks, please let the problem setter know.

Translated by ChatGPT 5