#include using namespace std; template vector LongestIncreasingSubsequence(vector &A, Compare comp) { int N = (int)A.size(); vector DP; vector nxt(N, N); vector mem(N); T inf; if (comp(0, numeric_limits::max())) inf = numeric_limits::min(); else inf = numeric_limits::max(); auto comp2 = [&](T l, T r) { return !comp(l, r); }; for (int i = N - 1; i >= 0; i--) { auto it = upper_bound(DP.begin(), DP.end(), A[i], comp2); int d = it - DP.begin(); if (it == DP.end()) DP.emplace_back(A[i]); else *it = A[i]; mem[i] = d; } stack st; for (int i = N - 1; i >= 0; i--) { while (!st.empty() && !comp(A[i], A[st.top()])) st.pop(); if (!st.empty()) nxt[i] = st.top(); st.emplace(i); } int len = (int)DP.size(); vector ret; for (int i = 0; len && i < N; i++) { if (mem[i] == len - 1) { ret.emplace_back(i); len--; } } return ret; } int main() { int n; cin >> n; vector v(n); for (int i = 0; i < n; i++) cin >> v[i]; auto lis_min = LongestIncreasingSubsequence(v, less()); reverse(v.begin(), v.end()); auto lis_max = LongestIncreasingSubsequence(v, greater()); reverse(lis_max.begin(), lis_max.end()); for (int i = 0; i < lis_max.size(); i++) lis_max[i] = n - 1 - lis_max[i]; reverse(v.begin(), v.end()); vector ans; int m = lis_min.size(); assert(int(lis_max.size()) == m); for (int i = 0; i < m; i++) { cerr << lis_min[i] << " " << lis_max[i] << '\n'; if (lis_min[i] == lis_max[i]) { ans.push_back(v[lis_min[i]]); } } cout << ans.size() << '\n'; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " \n"[i == int(ans.size()) - 1]; } }