#include <bits/stdc++.h>
using namespace ::std;
const int maxn = 2e5 + 500;
int a[maxn];
int cnt[maxn];
int l[maxn * 2];
int r[maxn * 2];
int main() {
  int n;
  cin >> n;
  int maxx = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxx = max(maxx, a[i]);
    a[i]--;
    cnt[a[i]]++;
  }
  int mx = 0;
  for (int i = 0; i < maxx; i++) {
    mx = max(mx, cnt[i]);
  }
  int b = 0, A;
  for (int i = 0; i < maxx; i++) {
    if (cnt[i] == mx) {
      b++;
      A = i;
    }
  }
  if (b > 1) {
    cout << n << endl;
    return 0;
  }
  int ans = 0;
  for (int i = 0; i < maxx; i++) {
    if (i != A) {
      int s = 0;
      memset(l, 31, sizeof l);
      memset(r, 0, sizeof r);
      l[maxn] = -1;
      r[maxn] = -1;
      for (int j = 0; j < n; j++) {
        if (a[j] == A) {
          s++;
        }
        if (a[j] == i) {
          s--;
        }
        l[s + maxn] = min(l[s + maxn], j);
        r[s + maxn] = max(r[s + maxn], j);
      }
      for (int j = -n; j < n; j++) {
        ans = max(ans, r[j + maxn] - l[j + maxn]);
      }
    }
  }
  cout << ans;
}
