310273: CF1808B. Playing in a Casino

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. Playing in a Casinotime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Galaxy Luck, a well-known casino in the entire solar system, introduces a new card game.

In this game, there is a deck that consists of $n$ cards. Each card has $m$ numbers written on it. Each of the $n$ players receives exactly one card from the deck.

Then all players play with each other in pairs, and each pair of players plays exactly once. Thus, if there are, for example, four players in total, then six games are played: the first against the second, the first against the third, the first against the fourth, the second against the third, the second against the fourth and the third against the fourth.

Each of these games determines the winner in some way, but the rules are quite complicated, so we will not describe them here. All that matters is how many chips are paid out to the winner. Let the first player's card have the numbers $a_1, a_2, \dots, a_m$, and the second player's card — $b_1, b_2, \dots, b_m$. Then the winner of the game gets $|a_1 - b_1| + |a_2 - b_2| + \dots + |a_m - b_m|$ chips from the total pot, where $|x|$ denotes the absolute value of $x$.

To determine the size of the total pot, it is necessary to calculate the winners' total winnings for all games. Since there can be many cards in a deck and many players, you have been assigned to write a program that does all the necessary calculations.

Input

Each test consists of several test cases. The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers $n$ and $m$ ($1 \le n \cdot m \le 3\cdot 10^5$) — the number of cards in the deck and the count of numbers on the one card.

Each of the following $n$ lines of the test case set contains $m$ integers $c_{i,j}$ ($1 \le c_{i,j} \le 10^6$) — a description of the $i$-th card.

It is guaranteed that the total $n \cdot m$ in all tests does not exceed $3 \cdot 10^5$.

Output

For each test case, print one number — the total amount of winnings from all games.

ExampleInput
3
3 5
1 4 2 8 5
7 9 2 1 4
3 8 5 3 1
1 4
4 15 1 10
4 3
1 2 3
3 2 1
1 2 1
4 2 7
Output
50
0
31
Note

Consider the first test case.

In the game between the first and second player, the winner receives $|1-7| + |4-9| + |2-2| + |8-1| + |5-4| = 19$ chips.

In the game between the first and third player, the winner receives $|1-3| + |4-8| + |2-5| + |8-3| + |5-1| = 18$ in chips.

In the game between the second and third player, the winner receives $|7-3| + |9-8| + |2-5| + |1-3| + |4-1| = 13$ chips.

The total is $19 + 18 + 13 = 50$ chips.

Input

题意翻译

### 题目描述 现有一种在太阳系中广为人知的卡牌游戏,叫做“Galaxy Luck”。 在每场游戏中,会有 $n$ 位玩家,每人 $1$ 张卡片,每张卡片上有 $m$ 个数字,每两位玩家会进行一次游戏,并且这两人之间只进行一次。例如:有四位玩家,第一位对第二位,第一位对第三位,第一位对第四位,第二位对第三位,第二位对第四位,第三位对第四位,共 $6$ 次游戏。 这种游戏有特定的获胜规则,赢家所获得的点数也有特定的计算方式;由于获胜规则很复杂,这里不会讲述。但更值得注意的是,应该给赢家多少点数。其遵循以下计算方式:第一位玩家有数字 $a_1,a_2,...,a_m$,第二位玩家有数字 $b_1,b_2,...,b_m$,那么赢家会得到的点数为:$|a_1-b_1|+|a_2-b_2|+\cdot\cdot\cdot+|a_m-b_m|$,其中, $|x|$ 代表 $x$ 的绝对值。 为了确定奖池的大小,需要编写一个程序来计算总点数。 ### 输入格式 每组中数据中包含多行。 输入中的第一行包含 $1$ 个整数 $t(1\le t\le1000)$,是总组数。接下来输入 $t$ 组。 每组中,第一行包含两个整数 $n$ 和 $m (1\le n\cdot m\le3\cdot 10^5)$,是卡片总数以及每张卡片上的数字数目。 接下来 $n$ 行,每行都包含 $m$ 个整数 $c_{i,j}(1\le c_{i,j}\le 10^6)$ ,是第 $i$ 张卡片上的数字。 数据保证所有的 $n\cdot m$ 都不会超过 $3\cdot 10^5$。 ### 输出格式 对于每组数据,打印 $1$ 个数字,用换行隔开,代表每场游戏的赢家点数总和。 ### 说明/提示 例如第一组数据: 第一位对第二位玩家,胜者得到 $|1-7|+|4-9|+|2-2|+|8-1|+|5-4|=19$ 点。 第一位对第三位玩家,胜者得到 $|1-3|+|4-8|+|2-5|+|8-3|+|5-1|=18$ 点。 第二位对第三位玩家,胜者得到 $|7-3|+|9-8|+|2-5|+|1-3|+|4-1|=13$ 点。 总点数为 $19+18+13=50$ 点。

加入题单

算法标签: