Under construction

Still being built. Things may change

Sequence Deletion

Time limit2sMemory limit1024 MB

Summary
Split sequence A into consecutive blocks so that the list of block mex values is lexicographically maximum, then print that list B.
Level

Medium7 of 10

Topics
Greedy, Array, Implementation
Solved
No attempts yet

Problem

Everyone knows the functions min⁡\min and max⁡\max, which return the smallest and largest value in a set. Now consider the function mex\text{mex}, which takes a set of numbers and returns the smallest non-negative integer that is not in the set. The name comes from minimal excluded. For example, mex({1,2,3})=0\text{mex}(\{1,2,3\}) = 0 and mex({0,1,2,4,5})=3\text{mex}(\{0,1,2,4,5\}) = 3. Magnus learned the definition of mex and immediately built a game on it. The player gets a sequence AA of NN non-negative integers and builds a sequence BB by repeating the following steps while AA still has elements: (1) Choose a positive integer kk that does not exceed the length of AA. (2) Append the mex of the first kk elements of AA to the end of BB. (3) Delete the first kk elements from AA. The player's task is to choose kk at each step so that the resulting sequence BB is lexicographically maximum among all possible choices. A sequence x=x1,x2,…,xnx = x_1, x_2, \ldots, x_n is lexicographically greater than y=y1,y2,…,ymy = y_1, y_2, \ldots, y_m if either there exists an ii with i≤ni \le n and i≤mi \le m such that x1=y1x_1 = y_1, x2=y2x_2 = y_2, …\ldots, xi−1=yi−1x_{i-1} = y_{i-1} and xi>yix_i > y_i, or n>mn > m and x1=y1x_1 = y_1, x2=y2x_2 = y_2, …\ldots, xm=ymx_m = y_m.

Input

The first line contains the length NN of sequence AA (1≤N≤500,0001 \le N \le 500,000). The second line contains the NN elements AiA_i (0≤Ai≤N0 \le A_i \le N), separated by spaces.

Output

Print the length MM of the found sequence BB on the first line. On the second line, print the elements B1,B2,…,BMB_1, B_2, \ldots, B_M separated by spaces.

Examples2

  1. Example 1

    Input
    5
    1 0 2 0 3
    
    Expected output
    1
    4
    
  2. Example 2

    Input
    8
    2 2 3 4 0 1 2 0
    
    Expected output
    2
    5 1