CF1921D.Very Different Array

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Petya has an array aia_i of nn integers. His brother Vasya became envious and decided to make his own array of nn integers.

To do this, he found mm integers bib_i (m≥nm\ge n), and now he wants to choose some nn integers of them and arrange them in a certain order to obtain an array cic_i of length nn.

To avoid being similar to his brother, Vasya wants to make his array as different as possible from Petya's array. Specifically, he wants the total difference D=∑i=1n∣ai−ci∣D = \sum_{i=1}^{n} |a_i - c_i| to be as large as possible.

Help Vasya find the maximum difference DD he can obtain.

彼得亚有一个包含 nn 个整数的数组 aia_i。他的弟弟瓦夏心生嫉妒,决定也构造一个长度为 nn 的整数数组。

为此,他找来了 mm 个整数 bib_i(其中 m≥nm \ge n),现在他想从中选出 nn 个数,并以某种顺序排列,构成一个长度为 nn 的数组 cic_i。

为了避免与哥哥的数组过于相似,瓦夏希望自己的数组与彼得亚的数组尽可能不同。具体而言,他希望总差异值 D=∑i=1n∣ai−ci∣D = \sum_{i=1}^{n} |a_i - c_i| 尽可能大。

请帮助瓦夏求出他所能得到的最大差异值 DD。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1001 \le t \le 100) — the number of test cases. This is followed by a description of the test cases.

The first line of each test case contains two integers nn and mm (1≤n≤m≤2⋅1051\le n\le m\le 2 \cdot 10^5).

The second line of each test case contains nn integers aia_i (1≤ai≤1091\le a_i\le 10^9). The third line of each test case contains mm integers bib_i (1≤bi≤1091\le b_i\le 10^9).

It is guaranteed that in a test, the sum of mm over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1001 \le t \le 100),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n≤m≤2⋅1051\le n\le m\le 2 \cdot 10^5)。

每个测试用例的第二行包含 nn 个整数 aia_i(1≤ai≤1091\le a_i\le 10^9)。每个测试用例的第三行包含 mm 个整数 bib_i(1≤bi≤1091\le b_i\le 10^9)。

保证在一个测试中,所有测试用例的 mm 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer — the maximum total difference DD that can be obtained.

对于每个测试用例,输出一个整数——所能获得的最大总差值 DD。

输入输出样例

  • 输入#1

    9
    4 6
    6 1 2 4
    3 5 1 7 2 3
    3 4
    1 1 1
    1 1 1 1
    5 5
    1 2 3 4 5
    1 2 3 4 5
    2 6
    5 8
    8 7 5 8 2 10
    2 2
    4 1
    9 6
    4 6
    8 10 6 4
    3 10 6 1 8 9
    3 5
    6 5 2
    1 7 9 7 2
    5 5
    9 10 6 3 7
    5 9 2 3 9
    1 6
    3
    2 7 10 1 1 5

    输出#1

    16
    0
    12
    11
    10
    23
    15
    25
    7

说明/提示

In the first example, Vasya can, for example, create the array (1,5,7,2)(1, 5, 7, 2). Then the total difference will be D=∣6−1∣+∣1−5∣+∣2−7∣+∣4−2∣=5+4+5+2=16D = |6-1|+|1-5|+|2-7|+|4-2| = 5+4+5+2 = 16.

In the second example, all the integers available to Vasya are equal to 1, so he can only create the array (1,1,1)(1, 1, 1), for which the difference D=0D = 0.

In the third example, Vasya can, for example, create the array (5,4,3,2,1)(5, 4, 3, 2, 1). Then the total difference will be D=∣1−5∣+∣2−4∣+∣3−3∣+∣4−2∣+∣5−1∣=4+2+0+2+4=12D = |1-5|+|2-4|+|3-3|+|4-2|+|5-1| = 4+2+0+2+4 = 12.

在第一个例子中,瓦西娅可以例如构造数组 (1,5,7,2)(1, 5, 7, 2)。此时总差值为 D=∣6−1∣+∣1−5∣+∣2−7∣+∣4−2∣=5+4+5+2=16D = |6-1|+|1-5|+|2-7|+|4-2| = 5+4+5+2 = 16。

在第二个例子中,瓦西娅可使用的所有整数均为 11,因此他只能构造数组 (1,1,1)(1, 1, 1),此时差值 D=0D = 0。

在第三个例子中,瓦西娅可以例如构造数组 (5,4,3,2,1)(5, 4, 3, 2, 1)。此时总差值为 D=∣1−5∣+∣2−4∣+∣3−3∣+∣4−2∣+∣5−1∣=4+2+0+2+4=12D = |1-5|+|2-4|+|3-3|+|4-2|+|5-1| = 4+2+0+2+4 = 12。

输入解题思路,AI测评打分。不知道怎么写?

首页