#include <bits/stdc++.h>
using namespace std;
int bs(int s, vector<int> &ac, int cc) {
  int e = ac.size() - 1, mid, last = ac.size();
  while (s <= e) {
    mid = (s + e) >> 1;
    if (ac[mid] <= cc)
      s = mid + 1;
    else
      e = mid - 1, last = mid;
  }
  return last;
}
vector<int> v;
vector<int> ac[4100];
set<pair<long long, long long> > ss;
map<int, int> mind;
int cl = 1;
int main() {
  int n;
  cin >> n;
  v.resize(n);
  int mx = 1;
  for (int i = (0); i < (n); i += 1) {
    cin >> v[i];
    int x = v[i];
    if ((v[i] = mind[v[i]]) == 0) mind[x] = v[i] = cl++;
    ac[v[i]].push_back(i);
    mx = max(mx, (int)ac[v[i]].size());
  }
  for (int i = (0); i < (4100); i += 1) {
    if (ac[i].size() == 0) continue;
    int cc[4100] = {0};
    int viz[4100];
    memset(viz, -1, sizeof(viz));
    cl = 0;
    for (int j = (0); j < (ac[i].size()); j += 1) {
      int s;
      if (j != 0)
        s = ac[i][j - 1] + 1;
      else
        s = 0;
      for (int x = (ac[i][j] - 1); x > (s - 1); x -= 1) {
        if (viz[v[x]] != j) {
          viz[v[x]] = j;
          cc[v[x]]++;
        }
      }
    }
    for (int x = (0); x < (4100); x += 1) {
      if (viz[x] == -1 || x == i) continue;
      int rame = bs(0, ac[x], ac[i][viz[x]]);
      cc[x] *= 2;
      rame = rame < ac[x].size();
      mx = max(mx, cc[x] + rame);
    }
  }
  cout << mx;
  return 0;
}
