#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int N = 5e5 + 10;
const long long mod = 1e9 + 7;
const long long mod2 = 998244353;
const long long inf = 8e18;
const int LOG = 22;
long long pw(long long a, long long b, long long M) {
  return (!b ? 1
             : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M
                      : pw(a * a % M, b / 2, M)));
}
int A[N], dp[N], Best[N], seg[4 * N], lz[4 * N], par[N], Real[N];
vector<int> vec[N];
inline void second(int v, int tl, int tr) {
  if (tl == tr) return;
  seg[v << 1] = max(seg[v << 1], lz[v]);
  seg[v << 1 | 1] = max(seg[v << 1 | 1], lz[v]);
  lz[v << 1] = max(lz[v << 1], lz[v]);
  lz[v << 1 | 1] = max(lz[v << 1 | 1], lz[v]);
  lz[v] = 0;
}
void U(int v, int tl, int tr, int l, int r, int x) {
  if (l > r) return;
  second(v, tl, tr);
  if (tl == l && tr == r) {
    lz[v] = max(x, lz[v]);
    seg[v] = max(seg[v], x);
    return;
  }
  int mid = (tl + tr) >> 1;
  if (l <= mid) {
    U(v << 1, tl, mid, l, min(r, mid), x);
  }
  if (r > mid) {
    U(v << 1 | 1, mid + 1, tr, max(mid + 1, l), r, x);
  }
  seg[v] = max(seg[v << 1], seg[v << 1 | 1]);
}
int G(int v, int tl, int tr, int l, int r) {
  second(v, tl, tr);
  if (tl == l && tr == r) {
    return seg[v];
  }
  int mid = (tl + tr) >> 1;
  int ans = 0;
  if (l <= mid) {
    ans = G(v << 1, tl, mid, l, min(r, mid));
  }
  if (r > mid) {
    ans = max(ans, G(v << 1 | 1, mid + 1, tr, max(mid + 1, l), r));
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  vector<int> cm;
  cm.push_back(0);
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    cm.push_back(A[i]);
  }
  sort((cm).begin(), (cm).end());
  for (int i = 1; i <= n; i++) {
    int id = lower_bound((cm).begin(), (cm).end(), A[i]) - cm.begin();
    Real[id] = A[i];
    A[i] = id;
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    par[i] = -1;
    if (vec[A[i]].empty()) {
      vec[A[i]].push_back(i);
      continue;
    }
    int Last = vec[A[i]].back();
    int sz = (int)vec[A[i]].size();
    if (sz > 2) {
      dp[i] = max(dp[i], 4 + dp[vec[A[i]][sz - 3] - 1]);
      if (dp[i] == 4 + dp[vec[A[i]][sz - 3] - 1]) {
        par[i] = vec[A[i]][sz - 3];
      }
    }
    int T = G(1, 1, n, Last, Last);
    Best[A[i]] = max(Best[A[i]], T);
    U(1, 1, n, Last + 1, i - 1, Last);
    vec[A[i]].push_back(i);
    if (Best[A[i]] == 0) continue;
    dp[i] = max(dp[i], 4 + dp[Best[A[i]] - 1]);
    if (dp[i] == 4 + dp[Best[A[i]] - 1]) {
      par[i] = Best[A[i]];
    }
  }
  cout << dp[n] << endl;
  int i = n;
  vector<int> ans;
  while (i > 0) {
    if (par[i] == -1) {
      i--;
      continue;
    }
    ans.push_back(A[i]);
    ans.push_back(A[par[i]]);
    ans.push_back(A[i]);
    ans.push_back(A[par[i]]);
    i = par[i] - 1;
  }
  reverse((ans).begin(), (ans).end());
  for (auto x : ans) {
    cout << Real[x] << " ";
  }
  cout << endl;
  return 0;
}
