cswork / OAOpen practice workspace

AmazonOA #31

Find Final Cart

Estimated MediumJudge-ready

Topics (AI-labelled)HashingIn-place Arrays

The cart starts with the items in the given order. A positive query x appends x to the end; a negative query −x removes the first occurrence of x currently in the cart. Output the final cart. This site's test cases guarantee that the item exists at every removal, and the behavior of removing a missing item, which the source does not specify, is left undefined.

Input

The first line contains n q (both 1..200000), the second line contains n initial items (1..10⁹), and the third line contains q non-zero queries (−10⁹..10⁹). The item of every removal query is currently present.

Output

First output the final number of items, then the items in order; if the cart is empty, output only 0.

Example 1

Input:

5 5
1 2 1 2 1
-1 -1 3 4 -3

Output:

4 2 2 1 4

Example 2

Input:

1 1
1
-1

Output:

0

Example 3

Input:

3 2
2 1 2
-2 2

Output:

3 1 2 2

Example notes

Example 1: after removing the first two occurrences of 1, [2,2,1] remains; append 3 and 4, then remove 3, ending with [2,2,1,4], so output the count 4 first. Example 2: after removing the only item, output 0. Example 3: remove the first 2, then append 2, giving [1,2,2], so output 3 1 2 2.

Editorial

Loading solution…