#arc177e. E - Wrong Scoreboard

E - Wrong Scoreboard

Score: 10001000 points

问题陈述

在AtCoder世界巡回赛决赛2800中,共有NN名参赛者参加,共提出了五个问题。每个问题都分配了一个至少为1分的整数分数,并且问题编号是按照从问题1到问题5的非递减顺序排列的。没有部分分数。根据AtCoder的常规规则,排名如下。在这个问题中,我们不考虑多个参赛者具有相同总分和罚分的情况。

排名:总分更高的参赛者排名更高。如果出现平局,罚分较小的参赛者排名更高。

现在,负责报道决赛的记者青木注意到了以下信息:

  1. 参赛者人数NN
  2. 每位参赛者解决了哪些问题。如果Ai,j=1A_{i,j}=1,则表示第ii名参赛者(1iN1 \leq i \leq N)正确解决了问题jj,如果Ai,j=0A_{i,j}=0,则表示他们没有解决。
  3. 每位参赛者的排名。第ii名参赛者(1iN1 \leq i \leq N)排名第RiR_i

然而,当他开始写文章时,他意识到他没有记录分数和罚分。此外,他意识到他记录的信息可能存在不一致性。现在,请解决以下问题。

假设他正确记录了信息1和2。设DiD_i为参赛者ii1iN1 \leq i \leq N)的实际排名,并找到可能的最小总平方误差$(D_1 - R_1)^2 + (D_2 - R_2)^2 + \dots + (D_N - R_N)^2$。

您有TT个测试用例需要处理。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

In the AtCoder World Tour Finals 2800, NN contestants participated, and a total of five problems were presented. Each problem is assigned an integer score of at least 11 point, and the problems are numbered so that the scores are non-decreasing from problem 11 to problem 55. There are no partial points. Similar to the usual AtCoder rules, ranking is done as follows. In this problem, we do not consider the situation where multiple contestants have the same total score and penalty.

RankingThe contestant with the higher total score ranks higher. In case of a tie, the one with the smaller penalty ranks higher.

Now, Aoki, a reporter covering the finals, noted the following information:

  1. The number of participants NN.
  2. Which problems each contestant solved. Ai,j=1A_{i,j}=1 means the ii-th contestant (1iN)(1 \leq i \leq N) correctly solved problem jj, and Ai,j=0A_{i,j}=0 means they did not.
  3. The rank of each contestant. The ii-th contestant (1iN)(1 \leq i \leq N) was ranked RiR_i-th.

However, when he started writing the article, he realized he did not note the scores and penalties. Furthermore, he realized there might be inconsistencies in the information he noted. Now, solve the following problem.

Assume that he correctly noted information 1 and 2. Let DiD_i be the actual rank of contestant ii (1iN)(1 \leq i \leq N), and find the minimum possible total squared error $(D_1 - R_1)^2 + (D_2 - R_2)^2 + \dots + (D_N - R_N)^2$.

You have TT test cases to process.

Constraints

  • 1T1051 \leq T \leq 10^5
  • 2N3×1052 \leq N \leq 3 \times 10^5
  • Each of Ai,1,Ai,2,Ai,3,Ai,4,Ai,5A_{i,1}, A_{i,2}, A_{i,3}, A_{i,4}, A_{i,5} is 00 or 11 (1iN)(1 \leq i \leq N).
  • The sum of Ai,1,Ai,2,Ai,3,Ai,4,Ai,5A_{i,1}, A_{i,2}, A_{i,3}, A_{i,4}, A_{i,5} is at least 11 (1iN)(1 \leq i \leq N).
  • 1RiN1 \leq R_i \leq N (1iN)(1 \leq i \leq N)
  • R1,R2,,RNR_1, R_2, \dots, R_N are distinct.
  • The total value of NN across all test cases is at most 3×1053 \times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format. Here casei\mathrm{case}_i represents the ii-th test case (1iT)(1 \leq i \leq T).

TT

case1\mathrm{case}_1

case2\mathrm{case}_2

\vdots

caseT\mathrm{case}_T

Each test case is given in the following format:

NN

A1,1A_{1,1} A1,2A_{1,2} A1,3A_{1,3} A1,4A_{1,4} A1,5A_{1,5}

A2,1A_{2,1} A2,2A_{2,2} A2,3A_{2,3} A2,4A_{2,4} A2,5A_{2,5}

\vdots

AN,1A_{N,1} AN,2A_{N,2} AN,3A_{N,3} AN,4A_{N,4} AN,5A_{N,5}

R1R_1 R2R_2 \cdots RNR_N

Output

Print the answers.

Sample Input 1

6
4
0 1 1 0 0
1 0 0 1 0
1 1 0 1 0
1 0 1 0 0
1 2 3 4
8
0 1 0 0 0
1 1 0 1 0
0 1 1 0 1
1 0 0 0 0
1 1 0 1 0
0 1 0 0 0
0 0 0 1 0
0 1 1 1 1
7 4 2 8 3 6 5 1
6
1 1 0 0 0
0 0 1 0 0
1 1 1 0 0
0 0 0 1 0
1 1 1 1 0
0 0 0 0 1
1 2 3 4 5 6
6
1 1 0 0 0
0 0 1 0 0
1 1 1 0 0
0 0 0 1 0
1 1 1 1 0
0 0 0 0 1
6 5 4 3 2 1
20
0 0 0 0 1
0 0 1 0 0
1 1 0 0 1
1 0 1 0 1
0 0 0 1 1
0 0 1 1 1
1 1 1 1 0
1 1 0 1 0
0 0 1 1 0
1 0 1 0 0
0 1 0 0 1
0 1 1 1 1
1 1 1 1 1
0 1 0 1 0
1 0 0 0 1
1 1 1 0 0
0 1 1 1 0
0 0 0 1 0
1 1 1 0 1
1 1 0 1 1
7 18 3 5 19 11 13 2 4 10 14 15 17 6 16 9 8 12 1 20
15
0 0 1 1 0
0 0 0 1 0
0 0 0 0 1
0 0 1 1 1
1 1 0 0 1
0 1 1 1 0
1 1 1 1 1
0 1 1 0 1
1 1 0 1 0
1 0 0 1 1
1 0 1 0 0
1 1 0 1 1
0 1 0 1 0
1 1 0 0 0
0 1 0 0 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Sample Output 1

6
0
26
0
1054
428

This input contains six test cases. Let us explain the first one.

Consider the following scenario.

  • Problems 11, 22, 33, 44, 55 have 100100, 500500, 800800, 900900, 13001300 points, respectively.
  • Contestants 11, 22, 33, 44 have penalties of 9090, 8080, 7070, 6060 minutes, respectively.

Then, the ranking will be as shown in the table below, where the total squared error is (21)2+(32)2+(13)2+(44)2=6(2-1)^2 + (3-2)^2 + (1-3)^2 + (4-4)^2 = 6. There is no way to make the total squared error 55 or less, so the answer is 66.

ContestantProblem 1Problem 2Problem 3Problem 4Problem 5Total scorePenaltyRank
Contestant 1-500800--130090 minutes2nd
Contestant 2100--900-100080 minutes3rd
Contestant 3100500-900-150070 minutes1st
Contestant 4100-800--90060 minutes4th

Now, let us explain the second test case.

Consider the following scenario.

  • Problems 11, 22, 33, 44, 55 have 10001000, 14001400, 20002000, 20002000, 27182718 points, respectively.
  • Contestants 11, 22, \dots, 88 have penalties of 295295, 286286, 242242, 236236, 277277, 288288, 187187, 299299 minutes, respectively.

Then, the ranking will be as shown in the table below. For every ii (1iN)(1 \leq i \leq N), the rank of contestant ii is RiR_i, so the total squared error is 00.

ContestantProblem 1Problem 2Problem 3Problem 4Problem 5Total scorePenaltyRank
Contestant 1-1400---1400295 minutes7th
Contestant 210001400-2000-4400286 minutes4th
Contestant 3-14002000-27186118242 minutes2nd
Contestant 41000----1000236 minutes8th
Contestant 510001400-2000-4400277 minutes3rd
Contestant 6-1400---1400288 minutes6th
Contestant 7---2000-2000187 minutes5th
Contestant 8-14002000200027188118299 minutes1st