#include <bits/stdc++.h>
using namespace std;
const int MX = (1 << 19), inf = (1 << 29);
map<int, int> hashy;
int key, st, en;
int orig[MX], tree[MX << 2], hist[MX << 2];
void update(int x, int a, int b) {
  if (st > b || st < a) return;
  if (a == b) {
    tree[x] = key;
    return;
  }
  update(x * 2, a, (a + b) / 2);
  update(x * 2 + 1, (a + b) / 2 + 1, b);
  tree[x] = min(tree[x * 2], tree[x * 2 + 1]);
}
int query(int x, int a, int b) {
  if (st > b || en < a)
    hist[x] = inf;
  else if (a >= st && b <= en)
    hist[x] = tree[x];
  else
    hist[x] =
        min(query(x * 2, a, (a + b) / 2), query(x * 2 + 1, (a + b) / 2 + 1, b));
  return hist[x];
}
int nk7(int x, int a, int b) {
  if (a >= st && b <= en)
    return tree[x];
  else
    return hist[x];
}
int get(int x, int a, int b) {
  if (a == b) return a;
  if (nk7(x * 2, a, (a + b) / 2) <= nk7(x * 2 + 1, (a + b) / 2 + 1, b))
    return get(x * 2, a, (a + b) / 2);
  else
    return get(x * 2 + 1, (a + b) / 2 + 1, b);
}
struct Query {
  int key, st, en, idx;
  Query() {}
  Query(int key, int st, int en, int idx)
      : key(key), st(st), en(en), idx(idx) {}
};
bool operator<(const Query &A, const Query &B) { return A.key < B.key; }
vector<Query> Queries;
int n, arr[MX], last[MX], nxt[MX], best[MX];
int dp[MX];
vector<int> sol;
int calc(int x) {
  if (x == n + 1) return 0;
  int &ret = dp[x];
  if (ret != -1) return ret;
  ret = calc(x + 1);
  if (best[x]) ret = max(ret, 1 + calc(nxt[best[x]] + 1));
  return ret;
}
void find_sol(int x) {
  if (x == n + 1) return;
  int ret = dp[x];
  if (ret == calc(x + 1))
    find_sol(x + 1);
  else {
    sol.push_back(orig[arr[x]]);
    sol.push_back(orig[arr[best[x]]]);
    sol.push_back(orig[arr[x]]);
    sol.push_back(orig[arr[best[x]]]);
    find_sol(nxt[best[x]] + 1);
  }
}
int main() {
  scanf("%d", &n);
  for (int j = 1; j <= n; j++) {
    scanf("%d", &arr[j]);
    hashy[arr[j]] = 1;
  }
  int cc = 0;
  for (auto it : hashy) {
    hashy[it.first] = ++cc;
    orig[cc] = it.first;
  }
  for (int j = 1; j <= n; j++) arr[j] = hashy[arr[j]];
  for (int j = 1; j <= cc; j++) last[j] = inf;
  set<pair<int, int> > S;
  for (int j = n; j; j--) {
    nxt[j] = last[arr[j]];
    S.insert(make_pair(nxt[j], j));
    last[arr[j]] = j;
  }
  for (int j = 1; j <= n; j++) {
    st = j, key = nxt[j];
    update(1, 1, n);
    int cur = j, n1 = nxt[j];
    if (n1 == inf) continue;
    Queries.push_back(Query(n1 + 1, cur + 1, n1 - 1, j));
    int n2 = nxt[n1];
    if (n2 == inf) continue;
    Queries.push_back(Query(n2 + 1, cur + 1, n2 - 1, j));
    int n3 = nxt[n2];
    if (n3 == inf) continue;
    best[j] = n2;
  }
  sort(Queries.begin(), Queries.end());
  for (auto Q : Queries) {
    int idx = Q.idx, V = Q.key;
    while (!S.empty() && S.begin()->first < V) {
      st = S.begin()->second, key = inf;
      update(1, 1, n);
      S.erase(S.begin());
    }
    st = Q.st, en = Q.en;
    if (query(1, 1, n) == inf) continue;
    int G = get(1, 1, n);
    if (nxt[G] < nxt[best[idx]] || best[idx] == 0) best[idx] = G;
  }
  memset(dp, -1, sizeof(dp));
  cout << calc(1) * 4 << endl;
  find_sol(1);
  for (auto pp : sol) cout << pp << ' ';
}

### Prompt

Develop a solution in Cpp to the problem described below:

After you have read all the problems, probably, you think Alex is genius person. That's true! One day he came up with the following task.

Given a sequence of integer numbers a1, a2, ..., an. You are to find a longest sequence b1, b2, ..., b4m, that satisfies the following conditions:

  * b4k + 1 = b4k + 3 for all valid integer k; 
  * b4k + 2 = b4k + 4 for all valid integer k; 
  * sequence b is subsequence of a (not necessarily contiguous subsequence). 



And finally... Alex had given this complicated task to George, and George gave it to you. Help George to cope with the task.

Input

The first line contains a single integer n (1 ≤ n ≤ 5·105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

In the first line print a single integer 4m — the maximal possible length of required sequence b. In the second line print 4m integers b1, b2, ..., b4m, that is required sequence.

If there are multiple optimal answers you may print any of them.

Examples

Input

4
3 5 3 5


Output

4
3 5 3 5


Input

10
35 1 2 1 2 35 100 200 100 200


Output

8
1 2 1 2 100 200 100 200

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int MX = (1 << 19), inf = (1 << 29);
map<int, int> hashy;
int key, st, en;
int orig[MX], tree[MX << 2], hist[MX << 2];
void update(int x, int a, int b) {
  if (st > b || st < a) return;
  if (a == b) {
    tree[x] = key;
    return;
  }
  update(x * 2, a, (a + b) / 2);
  update(x * 2 + 1, (a + b) / 2 + 1, b);
  tree[x] = min(tree[x * 2], tree[x * 2 + 1]);
}
int query(int x, int a, int b) {
  if (st > b || en < a)
    hist[x] = inf;
  else if (a >= st && b <= en)
    hist[x] = tree[x];
  else
    hist[x] =
        min(query(x * 2, a, (a + b) / 2), query(x * 2 + 1, (a + b) / 2 + 1, b));
  return hist[x];
}
int nk7(int x, int a, int b) {
  if (a >= st && b <= en)
    return tree[x];
  else
    return hist[x];
}
int get(int x, int a, int b) {
  if (a == b) return a;
  if (nk7(x * 2, a, (a + b) / 2) <= nk7(x * 2 + 1, (a + b) / 2 + 1, b))
    return get(x * 2, a, (a + b) / 2);
  else
    return get(x * 2 + 1, (a + b) / 2 + 1, b);
}
struct Query {
  int key, st, en, idx;
  Query() {}
  Query(int key, int st, int en, int idx)
      : key(key), st(st), en(en), idx(idx) {}
};
bool operator<(const Query &A, const Query &B) { return A.key < B.key; }
vector<Query> Queries;
int n, arr[MX], last[MX], nxt[MX], best[MX];
int dp[MX];
vector<int> sol;
int calc(int x) {
  if (x == n + 1) return 0;
  int &ret = dp[x];
  if (ret != -1) return ret;
  ret = calc(x + 1);
  if (best[x]) ret = max(ret, 1 + calc(nxt[best[x]] + 1));
  return ret;
}
void find_sol(int x) {
  if (x == n + 1) return;
  int ret = dp[x];
  if (ret == calc(x + 1))
    find_sol(x + 1);
  else {
    sol.push_back(orig[arr[x]]);
    sol.push_back(orig[arr[best[x]]]);
    sol.push_back(orig[arr[x]]);
    sol.push_back(orig[arr[best[x]]]);
    find_sol(nxt[best[x]] + 1);
  }
}
int main() {
  scanf("%d", &n);
  for (int j = 1; j <= n; j++) {
    scanf("%d", &arr[j]);
    hashy[arr[j]] = 1;
  }
  int cc = 0;
  for (auto it : hashy) {
    hashy[it.first] = ++cc;
    orig[cc] = it.first;
  }
  for (int j = 1; j <= n; j++) arr[j] = hashy[arr[j]];
  for (int j = 1; j <= cc; j++) last[j] = inf;
  set<pair<int, int> > S;
  for (int j = n; j; j--) {
    nxt[j] = last[arr[j]];
    S.insert(make_pair(nxt[j], j));
    last[arr[j]] = j;
  }
  for (int j = 1; j <= n; j++) {
    st = j, key = nxt[j];
    update(1, 1, n);
    int cur = j, n1 = nxt[j];
    if (n1 == inf) continue;
    Queries.push_back(Query(n1 + 1, cur + 1, n1 - 1, j));
    int n2 = nxt[n1];
    if (n2 == inf) continue;
    Queries.push_back(Query(n2 + 1, cur + 1, n2 - 1, j));
    int n3 = nxt[n2];
    if (n3 == inf) continue;
    best[j] = n2;
  }
  sort(Queries.begin(), Queries.end());
  for (auto Q : Queries) {
    int idx = Q.idx, V = Q.key;
    while (!S.empty() && S.begin()->first < V) {
      st = S.begin()->second, key = inf;
      update(1, 1, n);
      S.erase(S.begin());
    }
    st = Q.st, en = Q.en;
    if (query(1, 1, n) == inf) continue;
    int G = get(1, 1, n);
    if (nxt[G] < nxt[best[idx]] || best[idx] == 0) best[idx] = G;
  }
  memset(dp, -1, sizeof(dp));
  cout << calc(1) * 4 << endl;
  find_sol(1);
  for (auto pp : sol) cout << pp << ' ';
}

```