#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, const char * argv[]) {
int n;
int a[100000], pivot[100000];
fill(pivot, pivot + 100000, 1);
cin >> n;
int max = 0, min = 1000000001;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] < max) {
pivot[i] = 0;
}
if (a[i] > max) {
max = a[i];
}
}
for (int i = n - 1; i >= 0; i--) {
if (a[i] > min) {
pivot[i] = 0;
}
if (a[i] < min) {
min = a[i];
}
}
int cnt = 0;
for (int i = 0; i < n; i++) {
if (pivot[i]) {
cnt++;
}
}
cout << cnt << endl;
int flag = 1;
for (int i = 0; i < n; i++) {
if (pivot[i]) {
if (flag) {
cout << a[i];
flag = 0;
}else{
cout << ' ' << a[i];
}
}
}
cout << endl;
return 0;
}