/* * Author: nskybytskyi * Time: 2022-01-07 14:20:00 */ #include using namespace std; const int inf = numeric_limits::max(); // https://cp-algorithms.com/sequences/longest_increasing_subsequence.html vector lis(vector const& a) { int n = a.size(); vector d(n + 1, inf), ends(n); d[0] = -inf; for (int i = 0; i < n; ++i) { int j = distance(d.begin(), upper_bound(d.begin(), d.end(), a[i])); ends[i] = j; if (d[j - 1] < a[i] && a[i] < d[j]) { d[j] = a[i]; } } return ends; } int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector p(n); for (auto& pi : p) { cin >> pi; } auto ends = lis(p); for (auto& pi : p) { pi = -pi; } reverse(p.begin(), p.end()); auto begins = lis(p); reverse(begins.begin(), begins.end()); for (auto& pi : p) { pi = -pi; } reverse(p.begin(), p.end()); const int mx = *max_element(ends.begin(), ends.end()); vector> elems(mx + 1); for (int i = 0; i < n; ++i) { if (begins[i] + ends[i] == mx + 1) { elems[begins[i]].push_back(p[i]); } } set unique; for (const auto& vec_elem : elems) { if (vec_elem.size() == 1) { unique.insert(vec_elem[0]); } } cout << unique.size() << "\n"; for (auto elem : unique) { cout << elem << " "; } cout << "\n"; return 0; }