시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
2 초 512 MB 16 7 7 46.667%

문제

정수 배열 A 와 B가 있다. A는 총 n개의 서로 다른 양의 정수를 포함하고 B는 총 m개의 서로 다른 양의 정수를 포함한다.

A, B를 이용해서 길이가 n인 새로운 배열 C를 만들어보자.

  • C[i] 는 배열 B에 있는 값 중 A[i] 에 가장 가까운 값 (절대값 차이가 가장 작은 값)으로 정의 된다. 
  • 만약 이 조건을 만족하는 값들이 여럿 있는 경우, 그 중 가장 크기가 작은 값으로 정의 된다.

예를 들어 A = [20, 5, 14, 9] 그리고 B = [16, 8, 12] 라고 해보자.

  • C[1] = 16 이다 - 왜냐하면 B[1] = 16이 A[1] = 20에 가장 가깝기 때문이다.
  • C[2] = 8 이다 - 왜냐하면 B[2] = 8이 A[2] = 5에 가장 가깝기 때문이다.
  • C[3] = 12 이다 - 왜냐하면 B[1] = 16 와 B[3] = 12 모두 A[3] = 14에 가장 가깝지만, B[3]의 값이 더 작기 때문이다.
  • C[4] = 8이다.

이 예제의 경우 C = [16, 8, 12, 8]으로 정의된다.

두 배열 A와 B가 주어졌을 때, 새로운 배열 C를 계산하여 배열 C에 포함된 값들의 합을 구하는 프로그램을 작성하시오.

입력

첫 줄에 테스트 케이스의 수 T (1 <= T <= 10)가 주어진다.

각 테스트 케이스는 세 줄에 걸쳐서 주어진다.

첫 줄에는 n과 m이 공백으로 구분되어 주어진다 (1 <= n, m <= 10^6).

두 번째 줄에는 공백으로 구분된 n개의 정수가 주어지며, A[1] 부터 A[n]을 나타낸다 (각각의 값은 1이상 10^9 이하이다).

세 번째 줄에는 공백으로 구분된 m개의 정수가 주어지며, B[1] 부터 B[m]을 나타낸다 (각각의 값은 1이상 10^9 이하이다).

앞서 언급한대로, A와 B는 각각 서로 다른 양의 정수들을 포함한 배열들이다.

출력

각 테스트 케이스에 대해 배열 C를 구하고 해당 배열의 모든 원소 합을 한 줄에 출력하시오.

예제 입력 1

3
4 3
20 5 14 9
16 8 12
3 4
16 8 12
20 5 14 9
3 3
1 2 3
2 3 4

예제 출력 1

44
37
7

힌트

첫 테스트 케이스는 문제에서 언급되었다.

두 번째 테스트 케이스의 경우 A = [16, 8, 12] 이고  B = [20, 5, 14, 9] 이다.

이 경우 배열 C는 [14, 9, 14] 이며 따라서 정답은 14+9+14 = 37이다.

세 번째 테스트의 경우 C = [2 2 3] 이며 정답은 7이다. 

W3sicHJvYmxlbV9pZCI6IjE3MTI0IiwicHJvYmxlbV9sYW5nIjoiMCIsInRpdGxlIjoiXHViNDUwIFx1YWMxY1x1Yzc1OCBcdWJjMzBcdWM1ZjQiLCJkZXNjcmlwdGlvbiI6IjxwPlx1YzgxNVx1YzIxOCBcdWJjMzBcdWM1ZjQgQSBcdWM2NDAgQlx1YWMwMCBcdWM3ODhcdWIyZTQuIEFcdWIyOTQgXHVjZDFkIG5cdWFjMWNcdWM3NTggXHVjMTFjXHViODVjIFx1YjJlNFx1Yjk3OCBcdWM1OTFcdWM3NTggXHVjODE1XHVjMjE4XHViOTdjIFx1ZDNlY1x1ZDU2OFx1ZDU1OFx1YWNlMCBCXHViMjk0IFx1Y2QxZCBtXHVhYzFjXHVjNzU4IFx1YzExY1x1Yjg1YyBcdWIyZTRcdWI5NzggXHVjNTkxXHVjNzU4IFx1YzgxNVx1YzIxOFx1Yjk3YyBcdWQzZWNcdWQ1NjhcdWQ1NWNcdWIyZTQuPFwvcD5cclxuXHJcbjxwPkEsIEJcdWI5N2MgXHVjNzc0XHVjNmE5XHVkNTc0XHVjMTFjIFx1YWUzOFx1Yzc3NFx1YWMwMCBuXHVjNzc4IFx1YzBjOFx1Yjg1Y1x1YzZiNCBcdWJjMzBcdWM1ZjQgQ1x1Yjk3YyBcdWI5Y2NcdWI0ZTRcdWM1YjRcdWJjZjRcdWM3OTAuPFwvcD5cclxuXHJcbjx1bD5cclxuXHQ8bGk+Q1tpXSBcdWIyOTQgXHViYzMwXHVjNWY0IEJcdWM1ZDAgXHVjNzg4XHViMjk0IFx1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