#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int oo = 1e9;
vector<int> v[1005];
int a[1005];
int n;
int dp[1005][550] = {0};
int solve(int len) {
  for (int i = 0; i < 1005; i++)
    for (int j = 0; j < 550; j++) dp[i][j] = -oo;
  for (int j = 0; j < 550; j++) dp[n][j] = len == 1 ? 0 : -oo;
  dp[n][510] = 0;
  for (int i = n - 1; i >= 0; i--) {
    long long m = 1 << a[i];
    for (int mask = 510; mask >= 0; mask--) {
      dp[i][mask] = max(dp[i][mask], dp[i + 1][mask]);
      if (mask & m) continue;
      int sz = v[i].size();
      if (sz >= len) {
        int j = v[i][len - 1] + 1;
        dp[i][mask] = max(dp[i][mask], dp[j][mask | m] + len);
      }
      if (sz >= len - 1 && len != 1) {
        int j = v[i][len - 2] + 1;
        dp[i][mask] = max(dp[i][mask], dp[j][mask | m] + len - 1);
      }
    }
  }
  return dp[0][0];
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      if (a[j] == a[i]) {
        v[i].push_back(j);
      }
    }
  }
  int maxx = 1;
  for (int len = 152; len >= 1; len--) {
    maxx = max(maxx, solve(len));
  }
  cout << maxx << endl;
  return 0;
}
