problems / 264

Suki and Biscuit's Shared Sniffing Spots

easy two-pointers arrays sorting 1000 ms · 256 MB

Suki and Biscuit's Shared Sniffing Spots

Suki, a black curly-coated dog with a grey beard and white paws, spent the morning in the park with her friend Biscuit. Every lamp post, bench and bush in the park has a numeric id, and each dog's collar logged the id of every spot it stopped to sniff. Both logs come out sorted in non-decreasing order, and a spot sniffed several times appears several times.

Suki wants to know which sniffs they had in common. Pair up equal ids between the two logs, each entry used at most once: if spot x appears p times in Suki's log and q times in Biscuit's log, it is a shared spot min(p, q) times. For example, a spot appearing 3 times in one log and 2 times in the other is shared twice.

Print all shared spots, with this multiplicity, in non-decreasing order.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 2·10^5): the lengths of Suki's and Biscuit's logs.

The second line contains n integers a_1 ≤ a_2 ≤ … ≤ a_n (1 ≤ a_i ≤ 10^9): Suki's log.

The third line contains m integers b_1 ≤ b_2 ≤ … ≤ b_m (1 ≤ b_j ≤ 10^9): Biscuit's log.

Output

On the first line print k, the number of shared spots counted with multiplicity.

On the second line print the k shared ids in non-decreasing order, separated by spaces. If k is 0, the second line is empty.

Notes

In the first example, spot 2 appears 3 times in Suki's log and twice in Biscuit's, so it is shared twice. Spot 5 appears once in Suki's log and three times in Biscuit's, so it is shared once. Spot 7 appears once in each. Spots 1, 4 and 9 appear in only one log.

In the second example the dogs never sniffed the same spot, so the answer is 0.

Example 1

Input

7 7
1 2 2 2 5 7 9
2 2 4 5 5 5 7

Output

4
2 2 5 7

Example 2

Input

3 4
1 3 5
2 4 6 8

Output

0

Hints · reveal one at a time if you're stuck

Hint 1

Both logs are already sorted. Compare just the first entry of each: if they differ, can the smaller one ever be shared? And watch the repeats: a plain set intersection would print spot 2 of the first example only once.

Hint 2

Walk through both logs at the same time, keeping one index into each (the two-pointer technique). Work out which index to move in each case, so that every entry is used at most once and no possible match is skipped.

report an issue Prefer your own editor? sooks prep 264 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.