#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N;
scanf("%d", &N);
vector<long long> nums;
vector<long long> aftersort;
for (int i = 0; i < N; i++)
{
long long t;
scanf("%lld", &t);
nums.push_back(t);
aftersort.push_back(t);
}
sort(aftersort.begin(), aftersort.end());
// int mapp[500010];
map<long long,int> mapp;
bool needprint[100010];
for (int i = 0; i < 100010; i++)
{
needprint[i] = true;
}
for (int i = 0; i < aftersort.size(); i++)
{
mapp[aftersort[i]] = i;
}
int on=-1, off=-1,current=0;
int cnt = 0;
vector<long long> ans;
while (current < nums.size())
{
if (aftersort[current] != nums[current])
{
for (int i = current; i <= mapp[nums[current]]; i++)
{
needprint[i] = false;
}
if(mapp[nums[current]]>current)
current = mapp[nums[current]] + 1;
}
else {
cnt++;
ans.push_back(nums[current]);
current++;
}
}
cout << cnt<<endl ;
for (int i = 0; i < ans.size(); i++)
{
if (i != 0)
{
printf(" ");
}
printf("%lld", ans[i]);
}
cout<<endl;
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N;
scanf("%d", &N);
vector<long long> nums;
vector<long long> aftersort;
long long leftmax[100010]={0};
long long rightmin[100010] = {0};
int cnt = 0;
vector<long long >ans;
for (int i = 0; i < N; i++)
{
int t;
scanf("%lld", &t);
nums.push_back(t);
aftersort.push_back(t);
}
leftmax[0] = nums[0];
for (int i = 1; i < N; i++)
{
leftmax[i] = max(leftmax[i - 1], nums[i]);
}
rightmin[N-1] = nums[N-1];
for (int i = N-2; i >=0; i--)
{
rightmin[i] = min(rightmin[i +1], nums[i]);
}
for (int i = 0; i < N; i++)
{
if (leftmax[i] <= nums[i] && nums[i] <= rightmin[i])
{
cnt++;
ans.push_back(nums[i]);
}
}
cout << cnt << endl;
for (int i = 0; i < ans.size(); i++)
{
printf("%lld", ans[i]);
if (i != ans.size() - 1)
{
printf(" ");
}
}
cout << endl;
return 0;
}