#C9289. Sorted Intersection of Two Lists

    ID: 53365 Type: Default 1000ms 256MiB

Sorted Intersection of Two Lists

Sorted Intersection of Two Lists

You are given two lists of integers, and your task is to compute their sorted intersection. The intersection of two lists consists of the elements that appear in both lists. If an element appears multiple times in both lists, it should be included in the result as many times as the minimum number of occurrences in either list.

Formally, let the two lists be \(A\) and \(B\). For any integer \(x\), if \(x\) appears \(c_A\) times in \(A\) and \(c_B\) times in \(B\), then \(x\) should appear \(\min(c_A, c_B)\) times in the output. The final result must be sorted in non-decreasing order.

inputFormat

The input is given in four lines:

  • The first line contains an integer \(N\), the number of elements in the first list.
  • The second line contains \(N\) space-separated integers representing the first list.
  • The third line contains an integer \(M\), the number of elements in the second list.
  • The fourth line contains \(M\) space-separated integers representing the second list.

outputFormat

Output the sorted intersection of the two lists as a sequence of space-separated integers. If there is no intersection, output an empty line.

## sample
6
3 4 5 6 7 8
6
5 6 7 8 9 10
5 6 7 8