/// Author : Nguyễn Thái Sơn - K18 - KHMT - UIT
/// Training ICPC 2023
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/trie_policy.hpp>
//#include <ext/rope>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimization("unroll-loops, no-stack-protector")
//#pragma GCC target("avx,avx2,fma")
//using namespace std;
//using namespace __gnu_pbds;
//using namespace __gnu_cxx;
#define fi first
#define se second
#define TASK "delete"
#define pb push_back
#define EL cout << endl
#define Ti20_ntson int main()
#define in(x) cout << x << endl
#define all(x) (x).begin(),(x).end()
#define getbit(x, i) (((x) >> (i)) & 1)
#define cntbit(x) __builtin_popcount(x)
#define FOR(i,l,r) for (int i = l; i <= r; i++)
#define FORD(i,l,r) for (int i = l; i >= r; i--)
#define Debug(a,n) for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> vii;
typedef unsigned long long ull;
typedef vector<vector<int>> vvi;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
const int N = 5005;
const int oo = INT_MAX;
const int mod = 1e9 + 7;
const int d4x[4] = {-1, 0, 1, 0} , d4y[4] = {0, 1, 0, -1};
const int d8x[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, d8y[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int n, dp[N][2], cnt[N], Val[N][N], a[N];
vector<int> Store[N];
inline void Read_Input() {
cin >> n;
FOR(i, 1, n) {
int u; cin >> u;
a[i] = u;
if (Store[u].size() == 0) Store[u].push_back(0);
Store[u].push_back(i);
}
}
inline void Solve() {
FOR(i, 1, n) {
set<int, greater<int>> S; S.clear();
for (int j = i; j <= n; j++) {
S.erase(cnt[a[j]]);
++cnt[a[j]];
S.insert(cnt[a[j]]);
int u = *S.begin();
Val[i][j] = max(0, 2 * u - (j - i + 1));
}
for (int j = i; j <= n; j++)
cnt[a[j]]--;
}
int Ans = 0;
FOR(i, 1, n) {
int cnt = Store[i].size();
if (cnt == 0) continue;
Store[i].push_back(n + 1);
FOR(j, 0, cnt)
dp[j][0] = dp[j][1] = -mod;
dp[0][0] = 1;
FOR(l, 0, cnt - 1)
FOR(j, 0, 1) {
FOR(r, l + 1, cnt) {
/// ko lay l + 1
int dis = Store[i][r] - Store[i][l];
if (r != cnt)
dp[r][(j + dis) & 1] = max(dp[r][(j + dis) & 1], dp[l][j] - Val[Store[i][l] + 1][Store[i][r]]);
if ((j + dis) & 1) {
dp[r][0] = max(dp[r][0], dp[l][j] + 1 - Val[Store[i][l] + 1][Store[i][r] - 1]);
}
}
}
Ans = max(Ans, dp[cnt][0]);
}
cout << max(Ans - 2, 0) << '\n';
}
Ti20_ntson {
freopen(TASK".INP","r",stdin);
freopen(TASK".OUT","w",stdout);
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T = 1;
while (T -- ) {
Read_Input();
Solve();
}
}
Ly8vIEF1dGhvciA6IE5ndXnhu4VuIFRow6FpIFPGoW4gLSBLMTggLSBLSE1UIC0gVUlUCi8vLyBUcmFpbmluZyBJQ1BDIDIwMjMKCiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+Ci8vI2luY2x1ZGUgPGV4dC9wYl9kcy9hc3NvY19jb250YWluZXIuaHBwPgovLyNpbmNsdWRlIDxleHQvcGJfZHMvdHJpZV9wb2xpY3kuaHBwPgovLyNpbmNsdWRlIDxleHQvcm9wZT4KCi8vI3ByYWdtYSBHQ0Mgb3B0aW1pemUoIk9mYXN0IikKLy8jcHJhZ21hIEdDQyBvcHRpbWl6YXRpb24oInVucm9sbC1sb29wcywgbm8tc3RhY2stcHJvdGVjdG9yIikKLy8jcHJhZ21hIEdDQyB0YXJnZXQoImF2eCxhdngyLGZtYSIpCgovL3VzaW5nIG5hbWVzcGFjZSBzdGQ7Ci8vdXNpbmcgbmFtZXNwYWNlIF9fZ251X3BiZHM7Ci8vdXNpbmcgbmFtZXNwYWNlIF9fZ251X2N4eDsKCi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CwgLTF9Owpjb25zdCBpbnQgZDh4WzhdID0gey0xLCAtMSwgMCwgMSwgMSwgMSwgMCwgLTF9LCBkOHlbOF0gPSB7MCwgMSwgMSwgMSwgMCwgLTEsIC0xLCAtMX07CgppbnQgbiwgZHBbTl1bMl0sIGNudFtOXSwgVmFsW05dW05dLCBhW05dOwp2ZWN0b3I8aW50PiBTdG9yZVtOXTsKCmlubGluZSB2b2lkIFJlYWRfSW5wdXQoKSB7CiAgICBjaW4gPj4gbjsKICAgIEZPUihpLCAxLCBuKSB7CiAgICAgICAgaW50IHU7IGNpbiA+PiB1OwogICAgICAgIGFbaV0gPSB1OwogICAgICAgIGlmIChTdG9yZVt1XS5zaXplKCkgPT0gMCkgU3RvcmVbdV0ucHVzaF9iYWNrKDApOwogICAgICAgIFN0b3JlW3VdLnB1c2hfYmFjayhpKTsKICAgIH0KfQoKaW5saW5lIHZvaWQgU29sdmUoKSB7CiAgICBGT1IoaSwgMSwgbikgewogICAgICAgIHNldDxpbnQsIGdyZWF0ZXI8aW50Pj4gUzsgUy5jbGVhcigpOwogICAgICAgIGZvciAoaW50IGogPSBpOyBqIDw9IG47IGorKykgewogICAgICAgICAgICBTLmVyYXNlKGNudFthW2pdXSk7CiAgICAgICAgICAgICsrY250W2Fbal1dOwogICAgICAgICAgICBTLmluc2VydChjbnRbYVtqXV0pOwogICAgICAgICAgICBpbnQgdSA9ICpTLmJlZ2luKCk7CiAgICAgICAgICAgIFZhbFtpXVtqXSA9IG1heCgwLCAyICogdSAtIChqIC0gaSArIDEpKTsKICAgICAgICB9CiAgICAgICAgZm9yIChpbnQgaiA9IGk7IGogPD0gbjsgaisrKQogICAgICAgICAgICBjbnRbYVtqXV0tLTsKICAgIH0KICAgIGludCBBbnMgPSAwOwogICAgRk9SKGksIDEsIG4pIHsKICAgICAgICBpbnQgY250ID0gU3RvcmVbaV0uc2l6ZSgpOwogICAgICAgIGlmIChjbnQgPT0gMCkgY29udGludWU7CiAgICAgICAgU3RvcmVbaV0ucHVzaF9iYWNrKG4gKyAxKTsKICAgICAgICBGT1IoaiwgMCwgY250KQogICAgICAgICAgICBkcFtqXVswXSA9IGRwW2pdWzFdID0gLW1vZDsKICAgICAgICBkcFswXVswXSA9IDE7CiAgICAgICAgRk9SKGwsIDAsIGNudCAtIDEpCiAgICAgICAgICAgIEZPUihqLCAwLCAxKSB7CiAgICAgICAgICAgICAgICBGT1IociwgbCArIDEsIGNudCkgewogICAgICAgICAgICAgICAgICAgIC8vLyBrbyBsYXkgbCArIDEKICAgICAgICAgICAgICAgICAgICBpbnQgZGlzID0gU3RvcmVbaV1bcl0gLSBTdG9yZVtpXVtsXTsKICAgICAgICAgICAgICAgICAgICBpZiAociAhPSBjbnQpCiAgICAgICAgICAgICAgICAgICAgZHBbcl1bKGogKyBkaXMpICYgMV0gPSBtYXgoZHBbcl1bKGogKyBkaXMpICYgMV0sIGRwW2xdW2pdIC0gVmFsW1N0b3JlW2ldW2xdICsgMV1bU3RvcmVbaV1bcl1dKTsKICAgICAgICAgICAgICAgICAgICBpZiAoKGogKyBkaXMpICYgMSkgewogICAgICAgICAgICAgICAgICAgICAgICBkcFtyXVswXSA9IG1heChkcFtyXVswXSwgZHBbbF1bal0gKyAxIC0gVmFsW1N0b3JlW2ldW2xdICsgMV1bU3RvcmVbaV1bcl0gLSAxXSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgQW5zID0gbWF4KEFucywgZHBbY250XVswXSk7CiAgICB9CiAgICBjb3V0IDw8IG1heChBbnMgLSAyLCAwKSA8PCAnXG4nOwp9CgpUaTIwX250c29uIHsKICAgIGZyZW9wZW4oVEFTSyIuSU5QIiwiciIsc3RkaW4pOwogICAgZnJlb3BlbihUQVNLIi5PVVQiLCJ3IixzdGRvdXQpOwogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7IGNpbi50aWUoMCk7IGNvdXQudGllKDApOwogICAgaW50IFQgPSAxOwogICAgd2hpbGUgKFQgLS0gKSB7CiAgICAgICAgUmVhZF9JbnB1dCgpOwogICAgICAgIFNvbHZlKCk7CiAgICB9Cn0KCgoK