#include #include #include using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(n); i++) vector LIS(vector A){ int n = A.size(); vector ord(n, -1); vector I(n+1, 1001001001); rep(i,n){ ord[i] = upper_bound(I.begin(), I.end(), A[i]) - I.begin(); A[ord[i]] = A[i]; } return ord; } int main(){ int N; cin >> N; vector A(N); rep(i,N){ cin >> A[i]; A[i]--; } auto lis1 = LIS(A); int lislen = 0; for(auto a : lis1) lislen = max(lislen, a); lislen += 1; reverse(A.begin(), A.end()); for(auto& a : A) a *= -1; auto lis2 = LIS(A); reverse(lis2.begin(), lis2.end()); for(auto& a : lis2) a = lislen - 1 - a; reverse(A.begin(), A.end()); for(auto& a : A) a *= -1; vector ans2(lislen, 0); rep(i,N) if(lis1[i] == lis2[i]) ans2[lis1[i]]++; int n = lis1.size(); vector ans; rep(i,n) if(lis1[i] == lis2[i]) if(ans2[lis2[i]] == 1) ans.push_back(A[i] + 1); cout << ans.size() << "\n"; rep(i,ans.size()){ if(i) cout << " "; cout << ans[i]; } cout << "\n"; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } } ios_do_not_sync_instance;