欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1825B.LuoTianyi and the Table
LuoTianyi and the Table
LuoTianyi gave an array of integers. She asks you to construct a table of size , filled with these numbers, and each element of the array must be used exactly once. Also she asked you to maximize the following value:
$$\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\left(\max\limits_{1 \le x \le i, 1 \le y \le j}a_{x,y}-\min\limits_{1 \le x \le i, 1 \le y \le j}a_{x,y}\right)$$This means that we consider subtables with the upper left corner in and the bottom right corner in (, ), for each such subtable calculate the difference of the maximum and minimum elements in it, then sum up all these differences. You should maximize the resulting sum.
Help her find the maximal possible value, you don't need to reconstruct the table itself.
Input
Each test consists of multiple test cases. The first line contains a single integer () — the number of test cases. The description of test cases follows.
The first line of each test case contains two integers and () — the number of rows and columns of the table.
The second line of each test case contains integers () — the numbers you can put in the table.
Note, that integers in the array can be negative.
It is guaranteed that the sum of over all test cases doesn't exceed .
Output
For each test case, output a single integer — the maximal value, that can be obtained.
Note
In the first test case, the table is follows:
| 4 | 1 |
|---|---|
| 1 | 3 |
In the subtable with the bottom right corner in , the difference of the maximal and minimal elements is .
In the subtable with the bottom right corner in , the difference of the maximal and minimal elements is .
In the subtable with the bottom right corner in , the difference of the maximal and minimal elements is .
In the subtable with the bottom right corner in , the difference of the maximal and minimal elements is .
Then the maximum possible value is .
In the second test case, all elements are equal, so all differences are , and the answer is .
Samples
5
2 2
1 3 1 4
2 2
-1 -1 -1 -1
2 3
7 8 9 -3 10 8
3 2
4 8 -3 0 -7 1
4 3
-32030 59554 16854 -85927 68060 -64460 -79547 90932 85063 82703 -12001 38762
9
0
64
71
1933711
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |