//
// main.cpp
// scmax
//
// Created by Darius Buhai on 10/11/2018.
// Copyright © 2018 Darius Buhai. All rights reserved.
//
/*
5|24 12 15 15 19
1 3 2 2 1
dp[i] = dp[i+1]
maximul de la j la n
ex2:
7 10 9 4 12 9 11 4 49 2 15
4 5 3 4 2 3 2 1 1 2 1
*/
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, x, p[100001], s[100001];
deque<int> sec;
int CautareBinara(int x)
{
int Sol = -1, Left = 0, Right = n;
while(Left <= Right)
{
int Mid = (Left+Right) / 2;
if(s[Mid] == x)
{
Sol = Mid;
break;
}
if(s[Mid] > x)
Right = Mid - 1;
if(s[Mid] < x)
Left = Mid + 1;
}
return Sol;
}
int main() {
fin>>n>>x;
for(int i=0;i<n;i++)
{
fin>>x;
while(!sec.empty() && x<=sec.back())
sec.pop_back();
sec.push_back(x);
p[i] = (int)sec.size();
}
fout<<sec.size()<<endl;
while(!sec.empty()){
fout<<sec.front()<<" ";
sec.pop_front();
}
return 0;
}