B - intersection Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 8

問題文

それぞれ N 個、M 個の正整数からなる 2 つの数列 A=(A_1,A_2, \dots , A_N)B=(B_1,B_2, \dots , B_M) が与えられます。

AB の両方に含まれる要素を全て求め、値の小さい順に出力してください。

制約

  • 1 \leq N,M \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • i \neq j ならば A_i \neq A_j
  • i \neq j ならば B_i \neq B_j
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

出力

条件を満たす要素を、値の小さい順に空白区切りで全て出力せよ。


入力例 1

6 4
60 50 40 30 20 10
53 60 25 40

出力例 1

40 60 

値の小さい順に出力する必要があるので、60 40 という提出は不正解になることに注意してください。


入力例 2

3 1
1 2 3
123

出力例 2


条件を満たす要素が存在しないこともあります。

Score : 8 points

Warning

Do not make any mention of this problem until October 2, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

You are given a sequence of N positive integers and another of M positive integers: A=(A_1,A_2, \dots , A_N) and B=(B_1,B_2, \dots , B_M).

Find all common elements in A and B, and print them in ascending order.

Constraints

  • 1 \leq N,M \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • A_i \neq A_j if i \neq j.
  • B_i \neq B_j if i \neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

Output

Print all the common elements in ascending order, with spaces in between.


Sample Input 1

6 4
60 50 40 30 20 10
53 60 25 40

Sample Output 1

40 60 

Be sure to print them in ascending order: printing 60 40 would be considered incorrect.


Sample Input 2

3 1
1 2 3
123

Sample Output 2


There may be no common elements.