#K49642. Minimal First K Elements Sum
Minimal First K Elements Sum
Minimal First K Elements Sum
You are given t test cases. In each test case, you are provided with two arrays a
and b
each of length n, and an integer k.
Your task is to merge the two arrays into one sorted array (in non-decreasing order) and then compute the sum of the first k elements of this merged array. This sum is the minimum possible sum achievable by any merging because the smallest elements are taken.
Input Constraints:
- 1 ≤ t ≤ 10
- 1 ≤ n ≤ 105
- 1 ≤ k ≤ 2n
- Array elements are integers.
Example:
If you have one test case with n = 5, k = 3, a = [1, 2, 3, 4, 5] and b = [5, 6, 7, 8, 9], the merged sorted array is [1,2,3,4,5,5,6,7,8,9] and the sum of the first 3 elements is 1 + 2 + 3 = 6.
The solution requires careful input handling from standard input (stdin) and output to standard output (stdout).
inputFormat
The input begins with an integer t on the first line, representing the number of test cases. For each test case, the following three lines are provided:
- First line: Two integers n and k, where n is the length of each array and k indicates the number of smallest elements to sum.
- Second line: n space-separated integers representing the first array
a
. - Third line: n space-separated integers representing the second array
b
.
All input is read from standard input (stdin).
outputFormat
For each test case, output a single integer on a new line—the sum of the first k elements from the sorted merged array.
All output should be written to standard output (stdout).
## sample3
5 3
1 2 3 4 5
5 6 7 8 9
4 2
4 10 15 14
7 1 1 2
6 4
11 12 13 14 15 16
1 2 3 4 5 6
6
2
10
</p>