#include <bits/stdc++.h>
int v[4000 + 1], u[4000 + 1], fr[4000 + 1], last[4000 + 1];
int cautbin(int x, int v[], int k) {
  int rez = 0;
  for (int pas = 1 << 14; pas; pas >>= 1)
    if ((rez + pas <= k) && (v[rez + pas] <= x)) rez += pas;
  return rez;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &v[i]);
    u[i] = v[i];
  }
  std::sort(u + 1, u + n + 1);
  int k = 1;
  for (int i = 2; i <= n; i++)
    if (u[i] != u[k]) u[++k] = u[i];
  for (int i = 1; i <= n; i++) v[i] = cautbin(v[i], u, k);
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    memset(fr, 0, sizeof fr);
    memset(last, 0, sizeof last);
    fr[v[i]] = 1;
    last[v[i]] = i;
    int maxfr = 0;
    for (int j = i + 1; j <= n; j++) {
      if (v[j] == v[i]) {
        fr[v[i]]++;
        if (fr[v[i]] > ans) ans = fr[v[i]];
        if (1 + maxfr > ans) ans = 1 + maxfr;
      } else {
        if (last[v[j]] < last[v[i]]) {
          fr[v[j]] += 2;
          if (fr[v[j]] > ans) ans = fr[v[j]];
          if (fr[v[j]] > maxfr) maxfr = fr[v[j]];
        }
      }
      last[v[j]] = j;
    }
  }
  printf("%d\n", ans);
  return 0;
}
