#arc177e. E - Wrong Scoreboard
E - Wrong Scoreboard
Score: points
问题陈述
在AtCoder世界巡回赛决赛2800中,共有名参赛者参加,共提出了五个问题。每个问题都分配了一个至少为1分的整数分数,并且问题编号是按照从问题1到问题5的非递减顺序排列的。没有部分分数。根据AtCoder的常规规则,排名如下。在这个问题中,我们不考虑多个参赛者具有相同总分和罚分的情况。
排名:总分更高的参赛者排名更高。如果出现平局,罚分较小的参赛者排名更高。
现在,负责报道决赛的记者青木注意到了以下信息:
- 参赛者人数。
- 每位参赛者解决了哪些问题。如果,则表示第名参赛者()正确解决了问题,如果,则表示他们没有解决。
- 每位参赛者的排名。第名参赛者()排名第。
然而,当他开始写文章时,他意识到他没有记录分数和罚分。此外,他意识到他记录的信息可能存在不一致性。现在,请解决以下问题。
假设他正确记录了信息1和2。设为参赛者()的实际排名,并找到可能的最小总平方误差$(D_1 - R_1)^2 + (D_2 - R_2)^2 + \dots + (D_N - R_N)^2$。
您有个测试用例需要处理。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
In the AtCoder World Tour Finals 2800, contestants participated, and a total of five problems were presented. Each problem is assigned an integer score of at least point, and the problems are numbered so that the scores are non-decreasing from problem to problem . 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:
- The number of participants .
- Which problems each contestant solved. means the -th contestant correctly solved problem , and means they did not.
- The rank of each contestant. The -th contestant was ranked -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 be the actual rank of contestant , 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 test cases to process.
Constraints
- Each of is or .
- The sum of is at least .
- are distinct.
- The total value of across all test cases is at most .
- All input values are integers.
Input
The input is given from Standard Input in the following format. Here represents the -th test case .
Each test case is given in the following format:
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 , , , , have , , , , points, respectively.
- Contestants , , , have penalties of , , , minutes, respectively.
Then, the ranking will be as shown in the table below, where the total squared error is . There is no way to make the total squared error or less, so the answer is .
Contestant Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Total score Penalty Rank Contestant 1 - 500 800 - - 1300 90 minutes 2nd Contestant 2 100 - - 900 - 1000 80 minutes 3rd Contestant 3 100 500 - 900 - 1500 70 minutes 1st Contestant 4 100 - 800 - - 900 60 minutes 4th
Now, let us explain the second test case.
Consider the following scenario.
- Problems , , , , have , , , , points, respectively.
- Contestants , , , have penalties of , , , , , , , minutes, respectively.
Then, the ranking will be as shown in the table below. For every , the rank of contestant is , so the total squared error is .
Contestant Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Total score Penalty Rank Contestant 1 - 1400 - - - 1400 295 minutes 7th Contestant 2 1000 1400 - 2000 - 4400 286 minutes 4th Contestant 3 - 1400 2000 - 2718 6118 242 minutes 2nd Contestant 4 1000 - - - - 1000 236 minutes 8th Contestant 5 1000 1400 - 2000 - 4400 277 minutes 3rd Contestant 6 - 1400 - - - 1400 288 minutes 6th Contestant 7 - - - 2000 - 2000 187 minutes 5th Contestant 8 - 1400 2000 2000 2718 8118 299 minutes 1st