#include<bits/stdc++.h>
using namespace std;

long long a, b, dp[1000006], t, k, bagi = 1e9 + 7;

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin>> t >> k;

	for(int i = 0; i <= 100000; i++){
		if(i >= k){
			dp[i]= (dp[i - 1] + dp[i - k]) % bagi;
		}
		else{
			dp[i] = 1;
		}
	}

	dp[0] = 0;
	for(int i = 2; i <= 100000; i++){
		(dp[i] += dp[i - 1]) % bagi;
	}

	while(t--){
		cin>> a >> b;
		cout<< (dp[b] - dp[a - 1]) % bagi << '\n';
	}
}


#include<bits/stdc++.h>
using namespace std;

long long a, b, dp[1000006], t, k, bagi = 1e9 + 7;

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin>> t >> k;

	for(int i = 0; i <= 100000; i++){
		if(i >= k){
			dp[i]= (dp[i - 1] + dp[i - k]) % bagi;
		}
		else{
			dp[i] = 1;
		}
	}

	for(int i = 2; i <= 100000; i++){
		(dp[i] += dp[i - 1]) % bagi;
	}
	dp[0] = 0;

	while(t--){
		cin>> a >> b;
		cout<< (dp[b] - dp[a - 1]) % bagi << '\n';
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll t, k, a, b, i, bagi = 1e9 + 7, dp[100006], hasil;

ll f(ll x){
	if(x == 0){
		return 1;
	}
	else if(dp[x] == -1){
		if(x >= k){
			dp[x] = (f(x - 1)) + (f(x - k)) % bagi;
		}
		else{
			dp[x] = f(x - 1) % bagi;
		}
	}
	return dp[x];
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin>>  t >> k;

	memset(dp, -1, sizeof(dp));

	for(i = 0; i <= 100000; i++){
		dp[i] = f(i);
	}

	while(t--){
		cin>> a >> b;
		hasil = 0;

		for(i = a; i <= b; i++){
			(hasil += dp[i]) % bagi;

		}
		cout<< hasil << '\n';
	}
}


//mood : sad

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace std;

const int MaxN = 5000;

vector<int> Element[1000005];
int Next[MaxN], a[MaxN];
int dp[MaxN][MaxN];
int Ans = 1, n;

int main()
{
    scanf("%d", &n);
    for(int i(1); i <= n; ++ i)
    {
        scanf("%d", &a[i]);
        Element[a[i]].push_back(i);
        int Size = Element[a[i]].size();
        if(Size > 1)
            Next[Element[a[i]][Size - 2]] = i;
    }
    for(int i(n); i; -- i)
    {
        int Cursor = Next[i];
        for(int j(i + 1); j <= n; ++ j)
        {
            for(; Cursor && Cursor <= j; Cursor = Next[Cursor]);

            dp[i][j] = max(dp[i][j], Cursor ? dp[j][Cursor] + 1 : 2);

            Ans = max(Ans, dp[i][j]);
        }
    }
    printf("%d\n", Ans);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <map>
#include <algorithm>

using namespace std;

map<int, int> dp[5000];
int a[5000];
int n;
int ans = 2;

void work(int i, int j, int x)
{
    map<int, int>::iterator it = dp[j].find(x);
    if(it == dp[j].end())
    {
        dp[i][-x] = max(dp[i][-x], 2);
        return;
    }
    dp[i][-x] = max(dp[i][-x], (*it).second + 1);
    ans = max(ans, (*it).second + 1);
}

int main()
{
    scanf("%d", &n);
    if(n == 1)
        ans = 1;
    for(int i(1); i <= n; ++ i)
        scanf("%d", &a[i]);
    for(int i(1); i <= n; ++ i)
        for(int j(1); j < i; ++ j)
        {
            work(i, j, a[i] - a[j]);
        }
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <map>
#include <algorithm>

using namespace std;

map<int, int> dp[5000];
int a[5000];
int n;
int ans = 2;

void work(int i, int j, int x)
{
    map<int, int>::iterator it = dp[j].find(x);
    if(it == dp[j].end())
    {
        dp[i][-x] = max(dp[i][-x], 2);
        return;
    }
    dp[i][-x] = max(dp[i][-x], (*it).second + 1);
    ans = max(ans, (*it).second + 1);
}

int main()
{
    scanf("%d", &n);
    if(n == 1)
        ans = 1;
    for(int i(1); i <= n; ++ i)
        scanf("%d", &a[i]);
    for(int i(1); i <= n; ++ i)
        for(int j(1); j < i; ++ j)
        {
            work(i, j, a[i] - a[j]);
            work(i, j, a[j] - a[i]);
        }
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <map>
#include <algorithm>

using namespace std;

map<int, int> dp[5000];
int a[5000];
int n;
int ans = 2;

void work(int i, int j, int x)
{
    map<int, int>::iterator it = dp[j].find(x);
    if(it == dp[j].end())
    {
        dp[i][-x] = max(dp[i][-x], 2);
        return;
    }
    dp[i][-x] = max(dp[i][-x], (*it).second + 1);
    ans = max(ans, (*it).second + 1);
}

int main()
{
    scanf("%d", &n);
    for(int i(1); i <= n; ++ i)
        scanf("%d", &a[i]);
    for(int i(1); i <= n; ++ i)
        for(int j(1); j < i; ++ j)
        {
            work(i, j, a[i] - a[j]);
            work(i, j, a[j] - a[i]);
        }
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <map>
#include <algorithm>

using namespace std;

map<int, int> dp[5000];
int a[5000];
int n;
int ans = 2;

void work(int i, int j, int x)
{
    map<int, int>::iterator it = dp[j].find(x);
    if(it == dp[j].end())
    {
        dp[i][-x] = 2;
        return;
    }
    dp[i][-x] = (*it).second + 1;
    ans = max(ans, (*it).second + 1);
}

int main()
{
    scanf("%d", &n);
    for(int i(1); i <= n; ++ i)
        scanf("%d", &a[i]);
    for(int i(1); i <= n; ++ i)
        for(int j(1); j < i; ++ j)
        {
            work(i, j, a[i] - a[j]);
            work(i, j, a[j] - a[i]);
        }
    printf("%d\n", ans);
    return 0;
}


//mood : sad

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace std;

const int MaxN = 5000;

vector<int> Element[1000005];
int Next[MaxN], a[MaxN];
int dp[MaxN][MaxN];
int Ans = 1, n;

int main()
{
    scanf("%d", &n);
    for(int i(1); i <= n; ++ i)
    {
        scanf("%d", &a[i]);
        Element[a[i]].push_back(i);
        int Size = Element[a[i]].size();
        if(Size > 1)
            Next[Element[a[i]][Size - 2]] = i;
    }
    for(int i(n); i; -- i)
        for(int j(i + 1); j <= n; ++ j)
        {
            int Cursor = i;
            for(; Cursor && Cursor <= j; Cursor = Next[Cursor]);

            dp[i][j] = max(dp[i][j], Cursor ? dp[j][Cursor] + 1 : 2);

            Ans = max(Ans, dp[i][j]);
        }
    printf("%d\n", Ans);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<iostream>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int,int> PII;
const double eps = 1e-7;
const double PI = acos(-1.0);
const int oo = 1<<29;

const int N = 4044;

int last[N][N];
int dp[N][N];

int main()
{
	int n; scanf("%d",&n);
	int a[N];
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	mem(last,0);
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=n;j++) {
			if(a[j]==a[i]) last[i][j]=j;
			else last[i][j]=last[i][j-1];
		}
	}
	int ans=0;
	for(int i=2;i<=n;i++) {
		for(int j=1;j<i;j++) {
			dp[i][j]=dp[j][last[i][j]]+1;
			ans=max(ans,dp[i][j]);
		}
	}
	printf("%d\n",ans+1);

	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<iostream>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int,int> PII;
const double eps = 1e-7;
const double PI = acos(-1.0);
const int oo = 1<<29;

const int N = 4044;

map<int,int> dp[N];

int main()
{
	int n; scanf("%d",&n);
	int a[N];
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) {
		dp[1][a[i]]=1;
	}
	int ans=1;
	for(int i=2;i<=n;i++) {
		for(int j=1;j<i;j++) {
			dp[i][a[j]]=dp[j][a[i]]+1;
			ans=max(ans,dp[i][a[j]]);
		}
		if(dp[i][a[i]]==0) dp[i][a[i]]=1;
	}
	printf("%d\n",ans);

	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<iostream>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int,int> PII;
const double eps = 1e-7;
const double PI = acos(-1.0);
const int oo = 1<<29;

const int N = 4044;

int last[N][N];
int dp[N][N];

int main()
{
	int n; scanf("%d",&n);
	int a[N];
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	mem(last,0);
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=n;j++) {
			if(a[j]==a[i]) last[i][j]=j;
			else last[i][j]=last[i][j-1];
		}
	}
	int ans=0;
	for(int i=2;i<=n;i++) {
		for(int j=1;j<i;j++) {
			if(a[j]!=a[i]) dp[i][j]=dp[j][last[i][j]]+1;
			else dp[i][i]=dp[j][j]+1;
			ans=max(ans,dp[i][j]);
			ans=max(ans,dp[i][i]);
		}
	}
	printf("%d\n",ans+1);

	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<iostream>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int,int> PII;
const double eps = 1e-7;
const double PI = acos(-1.0);
const int oo = 1<<29;

const int N = 4044;

map<int,int> dp[N];

int main()
{
	int n; scanf("%d",&n);
	int a[N];
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	int ans=0;
	for(int i=2;i<=n;i++) {
		for(int j=1;j<i;j++) {
			dp[i][a[j]]=dp[j][a[i]]+1;
			ans=max(ans,dp[i][a[j]]);
		}
	}
	printf("%d\n",ans+1);

	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
long f[4003][4003],la[100006],ne[4003],a[4006],b[1000006];
int main(){
	std::ios::sync_with_stdio(false);
	long i,j,k,n;
	cin >> n;
	if (n==1){
		cout << 1 << endl;
		return 0;
	}
	for (i=1;i<=n;++i)
		cin >> a[i];
	for (i=n;i>0;--i){
		ne[i]=la[a[i]];
		la[a[i]]=i;
		for (k=i+1,j=ne[i];j!=0;j=ne[j])
			for (;k<j;++k)
				if (b[a[k]]!=i){
					b[a[k]]=i;
					f[i][k]=f[k][j]+1;
				}
		for (;k<=n;++k)
			f[i][k]=2;
	}
/*	for (i=1;i<=n;++i,cout << endl)
		for (j=1;j<=n;++j)
			cout << f[i][j] << ' ';*/
	for (k=0,i=1;i<=n;++i)
		for (j=1;j<=n;++j)
			k=max(k,f[i][j]);
	cout << k << endl;
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
long f[4003][4003],la[100006],ne[4003],a[4006],b[1000006];
int main(){
	std::ios::sync_with_stdio(false);
	long i,j,k,m,n;
	cin >> n;
	for (i=1;i<=n;++i)
		cin >> a[i];
	for (i=n;i>0;--i){
		ne[i]=la[a[i]];
		la[a[i]]=i;
		for (k=i+1,j=ne[i];j!=0;j=ne[j])
			for (;k<j;++k)
				if (b[a[k]]!=i){
					b[a[k]]=i;
					f[i][k]=f[k][j]+1;
				}
		for (;k<=n;++k)
			f[i][k]=2;
	}
/*	for (i=1;i<=n;++i,cout << endl)
		for (j=1;j<=n;++j)
			cout << f[i][j] << ' ';*/
	for (k=0,i=1;i<=n;++i)
		for (j=1;j<=n;++j)
			k=max(k,f[i][j]);
	cout << k << endl;
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
long f[4003][4003],la[1000006],ne[4003],a[4006],b[1000006];
int main(){
	std::ios::sync_with_stdio(false);
	long i,j,k,n;
	cin >> n;
	if (n==1){
		cout << 1 << endl;
		return 0;
	}
	for (i=1;i<=n;++i)
		cin >> a[i];
	for (i=n;i>0;--i){
		ne[i]=la[a[i]];
		la[a[i]]=i;
		for (k=i+1,j=ne[i];j!=0;j=ne[j])
			for (;k<j;++k)
				if (b[a[k]]!=i){
					b[a[k]]=i;
					f[i][k]=f[k][j]+1;
				}
		for (;k<=n;++k)
			f[i][k]=2;
	}
/*	for (i=1;i<=n;++i,cout << endl)
		for (j=1;j<=n;++j)
			cout << f[i][j] << ' ';*/
	for (k=0,i=1;i<=n;++i)
		for (j=1;j<=n;++j)
			k=max(k,f[i][j]);
	cout << k << endl;
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<map>
using namespace std;
struct PAIR{
	long x,y;
}now,anow;
bool operator < (PAIR u,PAIR v){
	return u.x<v.x || u.x==v.x && u.y<v.y;
}
map <PAIR,long> f;
long a[4003],b[1000006],ans;
int main(){
	std::ios::sync_with_stdio(false);
	long i,j,k,m,n;
	cin >> n;
	for (i=1;i<=n;++i)
		cin >> a[i];
	for (i=n;i>0;--i)
		for (j=i+1;j<=n;++j)
			if (b[a[j]]!=i){
				now.x=a[i];
				now.y=a[j];
				anow.x=a[j];
				anow.y=a[i];
				f[now]=f[anow]+1;
				ans=max(ans,f[now]);
				b[a[j]]=i;
			}
	cout << (++ans) << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[4005];
vector<int> v[4005];
int tmp[40005];
int main()
{
	int n,i,j;
	scanf("%d",&n);
	map<int,int> gr,rev;
	vector<int> diff;
	int in = 0;
	for(i=0;i<n;i++){
	scanf("%d",&a[i]);
	if(gr.find(a[i])==gr.end())
	{
		rev[in] = a[i];
		gr[a[i]]=in++;
	}
	j = gr[a[i]];
	v[j].push_back(i);
	}
	map<int,int>::iterator I;
	for(I=gr.begin();I!=gr.end();I++)
	diff.push_back(I->second);
	int l = diff.size();
	if(l<=1)
	{
		printf("%d\n",n);
	return 0;
}
int ma = 0;
for(i=0;i<l;i++)
{
	for(j=0;j<l;j++)
	{
		int x,y;
		x = diff[i];
		y = diff[j];
	//	x = rev[x];
	//	y=rev[y];
	//	printf("considering %d and %d\n",rev[x],rev[y]);

	int u;

	int pr = -1;
	int l1 = v[x].size();
	int l2 = v[y].size();
//	printf("first has %d and second has %d\n",l1,l2);
	int inn = 0;
	for(u=0;u<l1;u++)tmp[inn++] = v[x][u];
	for(u=0;u<l2;u++)tmp[inn++] = v[y][u];
	sort(tmp,tmp+inn);

	int xx ;
	int cur = 0;
	for(xx=0;xx<inn;xx++)tmp[xx] = a[tmp[xx]];
	for(xx=0;xx<inn;xx++)
{
	if(tmp[xx]!=pr)cur++;
	pr = tmp[xx];
}
if(cur > ma)ma = cur;
	}
}
printf("%d\n",ma);
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[4005];
vector<int> v[4005];
int tmp[40005];
int main()
{
	int n,i,j;
	scanf("%d",&n);
	map<int,int> gr,rev;
	vector<int> diff;
	int in = 1;
	for(i=1;i<=n;i++){
	scanf("%d",&a[i]);
	if(gr.find(a[i])==gr.end())
	{
		rev[in] = a[i];
		gr[a[i]]=in++;
	}
	j = gr[a[i]];
	v[j].push_back(i);
	}
	map<int,int>::iterator I;
	for(I=gr.begin();I!=gr.end();I++)
	diff.push_back(I->second);
	int l = diff.size();
	if(l<=1)
	{
		printf("%d\n",n);
	return 0;
}
int ma = 0;
for(i=0;i<l;i++)
{
	for(j=i+1;j<l;j++)
	{
		int x,y;
		x = diff[i];
		y = diff[j];
	//	x = rev[x];
	//	y=rev[y];
	//	printf("considering %d and %d\n",rev[x],rev[y]);
int first,second;
first = second = 0;
	int u,vv;
	u = 0;
	vv = 0;
	int pr = -1;
	int l1 = v[x].size();
	int l2 = v[y].size();
//	printf("first has %d and second has %d\n",l1,l2);
	int inn = 0;
	while(1)
	{
		if(u>=l1)break;
		if(vv>=l2)break;
		if(v[x][u] < v[y][vv])
		{
			tmp[inn++] = rev[x];
			u++;
		}
		else {tmp[inn++] = rev[y];vv++;}
	}
	while(u<l1){tmp[inn++] = rev[x];u++;}
	while(vv<l2){tmp[inn++] = rev[y];vv++;}
	int xx ;
	int cur = 0;
	for(xx=0;xx<inn;xx++)
{
	if(tmp[xx]!=pr)cur++;
	pr = tmp[xx];
}
if(cur > ma)ma = cur;
	}
}
printf("%d\n",ma);
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[4005];
vector<int> v[4005];
int tmp[40005];
int main()
{
	int n,i,j;
	scanf("%d",&n);
	map<int,int> gr,rev;
	vector<int> diff;
	int in = 1;
	for(i=1;i<=n;i++){
	scanf("%d",&a[i]);
	if(gr.find(a[i])==gr.end())
	{
		rev[in] = a[i];
		gr[a[i]]=in++;
	}
	j = gr[a[i]];
	v[j].push_back(i);
	}
	map<int,int>::iterator I;
	for(I=gr.begin();I!=gr.end();I++)
	diff.push_back(I->second);
	int l = diff.size();
	if(l<=1)
	{
		printf("%d\n",n);
	return 0;
}
int ma = 0;
for(i=0;i<l;i++)
{
	for(j=i+1;j<l;j++)
	{
		int x,y;
		x = diff[i];
		y = diff[j];
	//	x = rev[x];
	//	y=rev[y];
	//	printf("considering %d and %d\n",rev[x],rev[y]);
int first,second;
first = second = 0;
	int u,vv;
	u = 0;
	vv = 0;
	int pr = -1;
	int l1 = v[x].size();
	int l2 = v[y].size();
//	printf("first has %d and second has %d\n",l1,l2);
	int inn = 0;
	for(u=0;u<l1;u++)tmp[inn++] = v[x][u];
	for(u=0;u<l2;u++)tmp[inn++] = v[y][u];
	sort(tmp,tmp+inn);

	int xx ;
	int cur = 0;
	for(xx=0;xx<inn;xx++)tmp[xx] = a[tmp[xx]];
	for(xx=0;xx<inn;xx++)
{
	if(tmp[xx]!=pr)cur++;
	pr = tmp[xx];
}
if(cur > ma)ma = cur;
	}
}
printf("%d\n",ma);
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[4005];
vector<int> v[4005];
int tmp[40005];
int main()
{
	int n,i,j;
	scanf("%d",&n);
	map<int,int> gr,rev;
	vector<int> diff;
	int in = 0;
	for(i=0;i<n;i++){
	scanf("%d",&a[i]);
	if(gr.find(a[i])==gr.end())
	{
		rev[in] = a[i];
		gr[a[i]]=in++;
	}
	j = gr[a[i]];
	v[j].push_back(i);
	}
	map<int,int>::iterator I;
	for(I=gr.begin();I!=gr.end();I++)
	diff.push_back(I->second);
	int l = diff.size();
	if(l<=1)
	{
		printf("%d\n",n);
	return 0;
}
int ma = 0;
for(i=0;i<l;i++)
{
	for(j=i+1;j<l;j++)
	{
		int x,y;
		x = diff[i];
		y = diff[j];
	//	x = rev[x];
	//	y=rev[y];
	//	printf("considering %d and %d\n",rev[x],rev[y]);

	int u;

	int pr = -1;
	int l1 = v[x].size();
	int l2 = v[y].size();
//	printf("first has %d and second has %d\n",l1,l2);
	int inn = 0;
	for(u=0;u<l1;u++)tmp[inn++] = v[x][u];
	for(u=0;u<l2;u++)tmp[inn++] = v[y][u];
	sort(tmp,tmp+inn);

	int xx ;
	int cur = 0;
	for(xx=0;xx<inn;xx++)tmp[xx] = a[tmp[xx]];
	for(xx=0;xx<inn;xx++)
{
	if(tmp[xx]!=pr)cur++;
	pr = tmp[xx];
}
if(cur > ma)ma = cur;
	}
}
printf("%d\n",ma);
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[4005];
vector<int> v[4005];
int tmp[40005];
int main()
{
	int n,i,j;
	scanf("%d",&n);
	map<int,int> gr,rev;
	vector<int> diff;
	int in = 0;
	for(i=0;i<n;i++){
	scanf("%d",&a[i]);
	if(gr.find(a[i])==gr.end())
	{
		rev[in] = a[i];
		gr[a[i]]=in++;
	}
	j = gr[a[i]];
	v[j].push_back(i);
	}
	map<int,int>::iterator I;
	for(I=gr.begin();I!=gr.end();I++)
	diff.push_back(I->second);
	int l = diff.size();
	if(l<=1)
	{
		printf("%d\n",n);
	return 0;
}
int ma = 0;
for(i=0;i<l;i++)
{
	if(v[diff[i]].size() > ma)ma = v[diff[i]].size();
	for(j=0;j<l;j++)
	{
		int x,y;
		x = diff[i];
		y = diff[j];
	//	x = rev[x];
	//	y=rev[y];
	//	printf("considering %d and %d\n",rev[x],rev[y]);

	int u;

	int pr = -1;
	int l1 = v[x].size();
	int l2 = v[y].size();
//	printf("first has %d and second has %d\n",l1,l2);
	int inn = 0;
	for(u=0;u<l1;u++)tmp[inn++] = v[x][u];
	for(u=0;u<l2;u++)tmp[inn++] = v[y][u];
	sort(tmp,tmp+inn);

	int xx ;
	int cur = 0;
	for(xx=0;xx<inn;xx++)tmp[xx] = a[tmp[xx]];
	for(xx=0;xx<inn;xx++)
{
	if(tmp[xx]!=pr)cur++;
	pr = tmp[xx];
}
if(cur > ma)ma = cur;
	}
}
printf("%d\n",ma);
return 0;
}


#include<stdio.h>
#include<algorithm>
#define rep(i, n)	for(int i = 0; i < n; i ++)

using namespace std;

const int N = 4010;
long long a[N];

int main () {
	int n, ans = 0;
	scanf("%d", &n);
	rep(i, n)		scanf("%I64d", a + i);
	rep(i, n - 1) {
		int q = a[i] - a[i + 1], f = -1, j;
		for(j = i + 1; j < n; j ++, f *= -1) {
			if(a[j] != a[j - 1] + f * 1LL * q)
				break;
		}
		ans = max(ans, j - i);
	}
	printf("%d\n", ans);
	return 0;
}


#include<stdio.h>
#include<algorithm>
#define rep(i, n)	for(int i = 0; i < n; i ++)

using namespace std;

const int N = 4010;
long long a[N];

int main () {
	int n, ans = 1;
	scanf("%d", &n);
	rep(i, n)		scanf("%I64d", a + i);
	rep(i, n - 1) {
		int q = a[i] - a[i + 1], f = -1, j, pre = a[i], cnt = 1;
		for(j = i + 1; j < n; j ++) {
			if(a[j] == pre + f * 1LL * q) {
				pre = a[j];
				f *= -1;
				cnt ++;
			}
		}
		ans = max(ans, cnt);
	}
	printf("%d\n", ans);
	return 0;
}


#include<stdio.h>
#include<algorithm>
#define rep(i, n)	for(int i = 0; i < n; i ++)

using namespace std;

const int N = 4010;
long long a[N];

int main () {
	int n, ans = 1;
	scanf("%d", &n);
	rep(i, n)		scanf("%I64d", a + i);
	rep(i, n - 1) {
		int q = a[i] - a[i + 1], f = -1, j;
		for(j = i + 1; j < n; j ++, f *= -1) {
			if(a[j] != a[j - 1] + f * 1LL * q)
				break;
		}
		ans = max(ans, j - i);
	}
	printf("%d\n", ans);
	return 0;
}


#include<stdio.h>
#include<algorithm>
#define rep(i, n)	for(int i = 0; i < n; i ++)

using namespace std;

const int N = 4010;
int a[N], cnt[N][N];

int main () {
	int n, ans = 1;
	scanf("%d", &n);
	rep(i, n)		scanf("%d", a + i);
	rep(i, n) {
		int last = -1;
		for(int j = 0; j < i; j ++) {
			if(~last) {
				cnt[j][i] = cnt[last][j] + 1;
			} else {
				cnt[j][i] = 2;
			}
			ans = max(ans, cnt[j][i]);
			if(a[i] == a[j])
				last = j;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <stdio.h>
#include <map>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstring>

#define MAXN 1000001
#define INFTY 1000000000

using namespace std;
int a[MAXN];

int main () {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int n, x;
    cin >> n;
    for(int i=0; i<n; i++){
            scanf("%d", &x);
            a[x]++;
    }
    sort(a, a+MAXN);
    cout<<a[MAXN-1]+a[MAXN-2]<<endl;
    return 0;
}


#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <stdio.h>
#include <map>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstring>

#define MAXN 4001
#define INFTY 1000000000

using namespace std;
int a[MAXN];
int f[MAXN][MAXN];

int b[MAXN];
map<int, int> M;

int main () {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int n, x;
    cin >> n;
    for(int i=0; i<n; i++){
            scanf("%d", &a[i]);
            b[i] = a[i];
    }
    sort(b, b+n);
    M[b[0]] = 1;
    for(int i=1; i<n; i++) if(b[i] > b[i-1]){
            M[b[i]] = M[b[i-1]]+1;
    }
    for(int i=0; i<n; i++) a[i] = M[a[i]];

    for(int j=1; j<4001; j++) f[0][j] = 1;
    int r = 1;
    for(int i=1; i<n; i++){
            for(int j=0; j<i; j++){
                  f[i][a[j]] = max(f[i][a[j]], f[j][a[i]]+1);
                  r = max(r, f[i][a[j]]);
            }
    }
    cout<<r<<endl;
    return 0;
}


#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <stdio.h>
#include <map>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstring>

#define MAXN 4001
#define INFTY 1000000000

using namespace std;
int a[MAXN];
int f[MAXN][MAXN];

int b[MAXN];
map<int, int> M;

int main () {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int n, x;
    cin >> n;
    for(int i=0; i<n; i++){
            scanf("%d", &a[i]);
            b[i] = a[i];
    }
    sort(b, b+n);
    M[b[0]] = 1;
    for(int i=1; i<n; i++) if(b[i] > b[i-1]){
            M[b[i]] = M[b[i-1]]+1;
    }
    for(int i=0; i<n; i++) a[i] = M[a[i]];

    for(int j=1; j<4001; j++) f[0][j] = 1;
    int r = 1;
    for(int i=1; i<n; i++){
            for(int j=0; j<=i; j++){
                  f[i][a[j]] = max(f[i][a[j]], f[j][a[i]]+1);
                  r = max(r, f[i][a[j]]);
            }
    }
    cout<<r<<endl;
    return 0;
}


#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <stdio.h>
#include <map>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstring>

#define MAXN 4001
#define INFTY 1000000000

using namespace std;
int a[MAXN];
int f[MAXN][MAXN];

int b[MAXN];
map<int, int> M;

int main () {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int n, x;
    cin >> n;
    for(int i=0; i<n; i++){
            scanf("%d", &a[i]);
            b[i] = a[i];
    }
    sort(b, b+n);
    M[b[0]] = 1;
    for(int i=1; i<n; i++) if(b[i] > b[i-1]){
            M[b[i]] = M[b[i-1]]+1;
    }
    for(int i=0; i<n; i++) a[i] = M[a[i]];

    for(int i=0; i<n; i++)
    for(int j=1; j<4001; j++) f[i][j] = 1;
    int r = 1;
    for(int i=1; i<n; i++){
            for(int j=0; j<i; j++){
                  f[i][a[j]] = max(f[i][a[j]], f[j][a[i]]+1);
                  r = max(r, f[i][a[j]]);
            }
    }
    cout<<r<<endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;
typedef long long ll;
const int MAXN = 4100;

int N;
int seq[MAXN];
int clist[MAXN];
vector <int> poss;
vector <int> llist[MAXN];

int solve (int x, int y)
{
	if (llist[x][0] > llist[y][0])
		swap (x, y);

	int count = 1;
	int xloc = 0, yloc = 0;
	while (true)
	{
		count++;
		while (xloc < llist[x].size() && llist[x][xloc] < llist[y][yloc])
			xloc++;
		if (xloc == llist[x].size())
		{
			break;
		}

		count++;
		while (yloc < llist[y].size() && llist[y][yloc] < llist[x][xloc])
			yloc++;
		if (yloc == llist[y].size())
		{
			break;
		}
	}
	return count;
}

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		cin >> seq[i];
		clist[i] = seq[i];
	}

	sort (clist, clist + N);

	for (int i = 0; i < N; i++)
	{
		if (i == 0 || clist[i] != clist[i-1])
			poss.push_back (clist[i]);
	}

	for (int i = 0; i < poss.size(); i++)
		for (int j = 0; j < N; j++)
			if (seq[j] == poss[i])
				llist[i].push_back (j);

	int ans = 1;
	for (int i = 0; i < poss.size(); i++)
	{
		ans = max (ans, llist[i].size());
		for (int j = i + 1; j < poss.size(); j++)
			ans = max (ans, solve (i, j));
	}

	cout << ans << "\n";
	return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;
typedef long long ll;
const int MAXN = 4100;

int N;
int seq[MAXN];
int clist[MAXN];
vector <int> poss;
vector <int> llist[MAXN];

int solve (int x, int y)
{
	if (llist[x][0] > llist[y][0])
		swap (x, y);

	int count = 1;
	int xloc = 0, yloc = 0;
	while (true)
	{
		count++;
		while (xloc < llist[x].size() && llist[x][xloc] < llist[y][yloc])
			xloc++;
		if (xloc == llist[x].size())
		{
			break;
		}

		count++;
		while (yloc < llist[y].size() && llist[y][yloc] < llist[x][xloc])
			yloc++;
		if (yloc == llist[y].size())
		{
			break;
		}
	}
	return count;
}

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		cin >> seq[i];
		clist[i] = seq[i];
	}

	sort (clist, clist + N);

	for (int i = 0; i < N; i++)
	{
		if (i == 0 || clist[i] != clist[i-1])
			poss.push_back (clist[i]);
	}

	for (int i = 0; i < poss.size(); i++)
		for (int j = 0; j < N; j++)
			if (seq[j] == poss[i])
				llist[i].push_back (j);

	int ans = 1;
	for (int i = 0; i < poss.size(); i++)
	{
		ans = max (ans, (int) llist[i].size());
		for (int j = i + 1; j < poss.size(); j++)
			ans = max (ans, solve (i, j));
	}

	cout << ans << "\n";
	return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;
typedef long long ll;
const int MAXN = 4100;

int N;
int seq[MAXN];
int clist[MAXN];
vector <int> poss;
vector <int> llist[MAXN];

int solve (int x, int y)
{
	if (llist[x][0] > llist[y][0])
		swap (x, y);

	int count = 1;
	int xloc = 0, yloc = 0;
	while (true)
	{
		count++;
		while (xloc < llist[x].size() && llist[x][xloc] < llist[y][yloc])
			xloc++;
		if (xloc == llist[x].size())
		{
			break;
		}

		count++;
		while (yloc < llist[y].size() && llist[y][yloc] < llist[x][xloc])
			yloc++;
		if (yloc == llist[y].size())
		{
			break;
		}
	}
	return count;
}

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		cin >> seq[i];
		clist[i] = seq[i];
	}

	sort (clist, clist + N);

	for (int i = 0; i < N; i++)
	{
		if (i == 0 || clist[i] != clist[i-1])
			poss.push_back (clist[i]);
	}

	for (int i = 0; i < poss.size(); i++)
		for (int j = 0; j < N; j++)
			if (seq[j] == poss[i])
				llist[i].push_back (j);

	int ans = 1;
	for (int i = 0; i < poss.size(); i++)
	{
		ans = mas (ans, llist[i].size());
		for (int j = i + 1; j < poss.size(); j++)
			ans = max (ans, solve (i, j));
	}

	cout << ans << "\n";
	return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;
typedef long long ll;
const int MAXN = 4100;

int N;
int seq[MAXN];
int clist[MAXN];
vector <int> poss;
vector <int> llist[MAXN];

int solve (int x, int y)
{
	if (llist[x][0] > llist[y][0])
		swap (x, y);

	int count = 1;
	int xloc = 0, yloc = 0;
	while (true)
	{
		count++;
		while (xloc < llist[x].size() && llist[x][xloc] < llist[y][yloc])
			xloc++;
		if (xloc == llist[x].size())
		{
			break;
		}

		count++;
		while (yloc < llist[y].size() && llist[y][yloc] < llist[x][xloc])
			yloc++;
		if (yloc == llist[y].size())
		{
			break;
		}
	}
	return count;
}

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		cin >> seq[i];
		clist[i] = seq[i];
	}

	sort (clist, clist + N);

	for (int i = 0; i < N; i++)
	{
		if (i == 0 || clist[i] != clist[i-1])
			poss.push_back (clist[i]);
	}

	for (int i = 0; i < poss.size(); i++)
		for (int j = 0; j < N; j++)
			if (seq[j] == poss[i])
				llist[i].push_back (j);

	int ans = 1;
	for (int i = 0; i < poss.size(); i++)
		for (int j = i + 1; j < poss.size(); j++)
			ans = max (ans, solve (i, j));

	cout << ans << "\n";
	return 0;
}


#include <stdio.h>
#include <map>

using namespace std;

int N;
int A[4005];

map <int, int> M, M1;

int main()
{
	int i, j;
	scanf("%d", &N);

	for(i = 0; i < N; ++i)
		scanf("%d", A + i);

	int sol = ((N >= 2) ? 2 : 1);

	for(i = 0; i < N; ++i)
	{
		int lsame = 1;
		for(j = i + 1; j < N; ++j)
			if(A[i] == A[j])
				++lsame;

		if(sol < lsame)
			sol = lsame;

		for(j = i + 1; j < N; ++j)
		{
			if(A[i] == A[j])
			{
				M1.clear();
			}
			else
			{
				if(M1[A[j]] == 0)
				{
					M[A[j]] = M[A[j]] + 1;
					M1[A[j]] = 1;
				}
			}
		}

		map <int, int> :: iterator mi;

		for(mi = M.begin(); mi != M.end(); ++mi)
		{
			int a = mi->first;
			int b = mi->second;

//			printf("i %d : %d %d\n", i, a, b);

			int res = 2 * b;
			if(M1[a] == 0)
				++res;

			if(sol < res)
				sol = res;
		}

		M.clear();
		M1.clear();
	}

	printf("%d\n", sol);

	return 0;
}


#include <stdio.h>
#include <map>

using namespace std;

int N;
int A[4005];
int B[4005][4005];

map <int, int> M;

int main()
{
	int i, j;

	scanf("%d", &N);
	j = 0;
	for(i = 0; i < N; ++i)
	{
		int a;
		scanf("%d", &a);
		if(M.find(a) == M.end())
			M[a] = j++;
		A[i] = M[a];
	}

	int sol = 0;

	for(i = 0; i < N; ++i)
	{
		for(j = 0; j < i; ++j)
		{
			int a = B[i][A[j]] = B[j][A[i]] + 1;

			if(sol < a)
				sol = a;
		}
	}

	printf("%d\n", sol + 1);

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 1;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size() / 2 * 2);
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = 0; j < z; j++) {
            if (i == j) continue;
            vi as[2] = {inds[i], inds[j]};
            int ci = as[0].back() > as[1].back() ? 0 : 1;
            int p = 1 << 30;
            int cnt = 0;
            while (as[ci].size()) {
                if (as[ci].back() < p) {
                    p = as[ci].back();
                    as[ci].pop_back();
                    ci = !ci;
                    cnt++;
                } else {
                    as[ci].pop_back();
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 1;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size());
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = i + 1; j < z; j++) {
            vi *as[2] = {&inds[i], &inds[j]};
            int pos[2] = {0, 0};
            int ci = (*as)[0][0] < (*as)[1][0] ? 0 : 1;
            int p = -1 << 29;
            int cnt = 0;
            while (pos[ci] < (*as)[ci].size()) {
                if ((*as)[ci][0] > p) {
                    p = (*as)[ci][pos[ci]];
                    pos[ci]++;
                    ci = !ci;
                    cnt++;
                } else {
                    pos[ci]++;
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 1;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size() / 2 * 2);
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = i + 1; j < z; j++) {
            vi as[2] = {inds[i], inds[j]};
            int ci = as[0].back() > as[1].back() ? 0 : 1;
            int p = 1 << 30;
            int cnt = 0;
            while (as[ci].size()) {
                if (as[ci].back() < p) {
                    p = as[ci].back();
                    as[ci].pop_back();
                    ci = !ci;
                    cnt++;
                } else {
                    as[ci].pop_back();
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 1;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size());
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = 0; j < z; j++) {
            if (i == j) continue;
            vi *as[2] = {&inds[i], &inds[j]};
            int pos[2] = {0, 0};
            int ci = (*as)[0][0] < (*as)[1][0] ? 0 : 1;
            int p = -1 << 28;
            int cnt = 0;
            while (pos[ci] < (*as)[ci].size()) {
                int t = (*as)[ci][pos[ci]++];
                if (t > p) {
                    p = t;
                    ci = !ci;
                    cnt++;
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 1;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size() / 2 * 2);
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = 0; j < z; j++) {
            if (i == j) continue;
            vi as[2] = {inds[i], inds[j]};
            int ci = 0;
            int p = 1 << 30;
            int cnt = 0;
            while (as[ci].size()) {
                if (as[ci].back() < p) {
                    p = as[ci].back();
                    as[ci].pop_back();
                    ci = !ci;
                    cnt++;
                } else {
                    as[ci].pop_back();
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 1;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size());
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = 0; j < z; j++) {
            vi *as[2] = {&inds[i], &inds[j]};
            int pos[2] = {0, 0};
            int ci = (*as[0])[0] < (*as[1])[0] ? 0 : 1;
            int p = -1 << 28;
            int cnt = 0;
            while (pos[ci] < (*as[ci]).size()) {
                int t = (*as[ci])[pos[ci]++];
                if (t > p) {
                    p = t;
                    ci = !ci;
                    cnt++;
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 0;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size() / 2 * 2);
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = i + 1; j < z; j++) {
            vi as[2] = {inds[i], inds[j]};
            int ci = as[0].back() > as[1].back() ? 0 : 1;
            int p = 1 << 30;
            int cnt = 0;
            while (as[ci].size()) {
                if (as[ci].back() < p) {
                    p = as[ci].back();
                    as[ci].pop_back();
                    ci = !ci;
                    cnt++;
                } else {
                    as[ci].pop_back();
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 1;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size());
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = i + 1; j < z; j++) {
            vi *as[2] = {&inds[i], &inds[j]};
            int pos[2] = {0, 0};
            int ci = (*as)[0][0] < (*as)[1][0] ? 0 : 1;
            int p = -1 << 29;
            int cnt = 0;
            while (pos[ci] < (*as)[ci].size()) {
                if ((*as)[ci][pos[ci]] > p) {
                    p = (*as)[ci][pos[ci]];
                    pos[ci]++;
                    ci = !ci;
                    cnt++;
                } else {
                    pos[ci]++;
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 1;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size());
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = i + 1; j < z; j++) {
            vi *as[2] = {&inds[i], &inds[j]};
            int ci = (*as)[0].back() > (*as)[1].back() ? 0 : 1;
            int p = 1 << 30;
            int cnt = 0;
            while ((*as)[ci].size()) {
                if ((*as)[ci].back() < p) {
                    p = (*as)[ci].back();
                    (*as)[ci].pop_back();
                    ci = !ci;
                    cnt++;
                } else {
                    (*as)[ci].pop_back();
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 1;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size());
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = 0; j < z; j++) {
            vi *as[2] = {&inds[i], &inds[j]};
            int pos[2] = {0, 0};
            int ci = (*as[ci])[0] < (*as[ci])[1] ? 0 : 1;
            int p = -1 << 28;
            int cnt = 0;
            while (pos[ci] < (*as[ci]).size()) {
                int t = (*as[ci])[pos[ci]++];
                if (t > p) {
                    p = t;
                    ci = !ci;
                    cnt++;
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 1;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size());
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = 0; j < z; j++) {
            if (i == j) continue;
            vi *as[2] = {&inds[i], &inds[j]};
            int pos[2] = {0, 0};
            int ci = 0;
            int p = -1 << 28;
            int cnt = 0;
            while (pos[ci] < (*as)[ci].size()) {
                int t = (*as)[ci][pos[ci]++];
                if (t > p) {
                    p = t;
                    ci = !ci;
                    cnt++;
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 1;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size());
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = i + 1; j < z; j++) {
            vi *as[2] = {&inds[i], &inds[j]};
            int pos[2] = {0, 0};
            int ci = (*as)[0][0] < (*as)[1][0] ? 0 : 1;
            int p = -1 << 28;
            int cnt = 0;
            while (pos[ci] < (*as)[ci].size()) {
                int t = (*as)[ci][pos[ci]++];
                if (t > p) {
                    p = t;
                    ci = !ci;
                    cnt++;
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;

int arr[4010];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    vector<vi> inds;
    int mx = 1;
    for (int i = 0; i < n; i++) {
        int t = arr[i];
        if (t == -1) continue;
        vi v;
        for (int j = i; j < n; j++) {
            if (arr[j] == t) {
                v.push_back(j);
                arr[j] = -1;
            }
        }
        mx = max(mx, (int)v.size());
        inds.push_back(v);
    }
    int z = inds.size();
    for (int i = 0; i < z; i++) {
        for (int j = i + 1; j < z; j++) {
            vi as[2] = {inds[i], inds[j]};
            int ci = as[0].back() > as[1].back() ? 0 : 1;
            int p = 1 << 30;
            int cnt = 0;
            while (as[ci].size()) {
                if (as[ci].back() < p) {
                    p = as[ci].back();
                    as[ci].pop_back();
                    ci = !ci;
                    cnt++;
                } else {
                    as[ci].pop_back();
                }
            }
            mx = max(mx, cnt);
        }
    }
    cout << mx << endl;
}


#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>

using namespace std;

#define mp make_pair
#define pb push_back
#define forn(i,n) for(int i=0;i<(n);i++)
#define dforn(i,n) for(int i=(n)-1;i>=0;i--)
#define all(v) v.begin(),v.end()

int b[5000],dp[5000][5000],ap[5000][5000];

int main()
{
    int n;
    cin>>n;
    forn(i,n)forn(j,n)dp[i][j]=0;
    forn(i,n)cin>>b[i];
    forn(i,n)
    {
        int u=-1;
        forn(j,n)
        {
            if(b[j]==b[i])
            {
                u=j;
            }
            ap[i][j]=u;
        }
    }
    /*forn(i,n)
    {
        forn(j,n)cout<<ap[i][j]<<" ";
        cout<<endl;
    }*/
    forn(i,n)forn(j,i)if(b[i]!=b[j])
    {
        dp[j][i]=2;
        if(ap[i][j]!=-1)dp[j][i]=1+dp[ap[i][j]][j];
    }
    /*forn(i,n)
    {
        forn(j,i)cout<<dp[j][i]<<" ";
        cout<<endl;
    }*/
    int res=0;
    forn(i,n)forn(j,i)res=max(res,dp[j][i]);
    cout<<res<<endl;
}


#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>

using namespace std;

#define mp make_pair
#define pb push_back
#define forn(i,n) for(int i=0;i<(n);i++)
#define dforn(i,n) for(int i=(n)-1;i>=0;i--)
#define all(v) v.begin(),v.end()

int b[5000],dp[5000][5000],ap[5000][5000];

int main()
{
    int n;
    cin>>n;
    forn(i,n)forn(j,n)dp[i][j]=0;
    forn(i,n)cin>>b[i];
    forn(i,n)
    {
        int u=-1;
        forn(j,n)
        {
            ap[i][j]=u;
            if(b[j]==b[i])
            {
                u=j;
            }
        }
    }
    /*forn(i,n)
    {
        forn(j,n)cout<<ap[i][j]<<" ";
        cout<<endl;
    }*/
    forn(i,n)forn(j,i)
    {
        dp[j][i]=2;
        if(ap[i][j]!=-1)dp[j][i]=1+dp[ap[i][j]][j];
    }
    /*forn(i,n)
    {
        forn(j,i)cout<<dp[j][i]<<" ";
        cout<<endl;
    }*/
    int res=0;
    forn(i,n)forn(j,i)res=max(res,dp[j][i]);
    if(n==1)cout<<1<<endl;else cout<<res<<endl;
}


#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>

using namespace std;

#define mp make_pair
#define pb push_back
#define forn(i,n) for(int i=0;i<(n);i++)
#define dforn(i,n) for(int i=(n)-1;i>=0;i--)
#define all(v) v.begin(),v.end()

int b[5000],dp[5000][5000],ap[5000][5000];

int main()
{
    int n;
    cin>>n;
    forn(i,n)forn(j,n)dp[i][j]=0;
    forn(i,n)cin>>b[i];
    forn(i,n)
    {
        int u=-1;
        forn(j,n)
        {
            ap[i][j]=u;
            if(b[j]==b[i])
            {
                u=j;
            }
        }
    }
    /*forn(i,n)
    {
        forn(j,n)cout<<ap[i][j]<<" ";
        cout<<endl;
    }*/
    forn(i,n)forn(j,i)
    {
        dp[j][i]=2;
        if(ap[i][j]!=-1)dp[j][i]=1+dp[ap[i][j]][j];
    }
    /*forn(i,n)
    {
        forn(j,i)cout<<dp[j][i]<<" ";
        cout<<endl;
    }*/
    int res=0;
    forn(i,n)forn(j,i)res=max(res,dp[j][i]);
    cout<<res<<endl;
}


#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>

using namespace std;

#define mp make_pair
#define pb push_back
#define forn(i,n) for(int i=0;i<(n);i++)
#define dforn(i,n) for(int i=(n)-1;i>=0;i--)
#define all(v) v.begin(),v.end()

int rev[2000000],b[5000],cnt[5000],u[5000];

int main()
{
    int n,q;
    cin>>n;
    int res=0;
    forn(i,n)cin>>b[i];
    forn(i,2000000)rev[i]=-1;
    forn(i,n)if(rev[b[i]]==-1)rev[b[i]]=i;
    forn(i,n)
    {
        forn(j,n)u[j]=cnt[j]=0;
        int k=0;
        forn(j,n)
        {
            if(b[j]==b[i])k++;
            else if(u[rev[b[j]]]<=k)
            {
                cnt[rev[b[j]]]++;
                u[rev[b[j]]]=k+1;
            }
        }
        int m=0;
        //forn(j,n)cout<<b[j]<<" "<<cnt[j]<<endl;
        forn(j,n)m=max(m,cnt[j]);
        if(m<k)res=max(res,2*m+1);
        if(m==k)res=max(res,2*m);
        if(m>k)res=max(res,2*k+1);
    }
    cout<<res<<endl;
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<cmath>
#include<deque>
#include<queue>

#define nx(x) (x | (x+1))
#define pr(x) (x & (x+1))

#define fi first
#define MAXN 222222
#define se second
#define sz(a) (int(a.size()))
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define forit(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

#define pb push_back
#define pf push_front
#define mem(a,b) memset(a,b,sizeof a)
#define eps 1e-8
using namespace std;
typedef long long ll;
vector<int> ps[1111111];
int siz[1111111];
map<pair<int,int>,int> was;
int main()
{
    //freopen("input.txt","r",stdin);
    int n,m,a[4444];
    mem(siz,0);
    set<int> s;
    cin>>n;
    for(int i=1;i<=n;i++)
      cin>>a[i],ps[a[i]].pb(i),siz[a[i]]++,s.insert(a[i]);

    set<int>::iterator i,j;

    int mx=-1;

    for(i=s.begin();;i++)
    {
      j=i;
      j++;
      if(j==s.end())break;
      for(;j!=s.end();j++)
      {
          int p=1;
          int k=0,k1=0,k2=0;
          int e1=*i,e2=*j;
          if(ps[e2][0]<ps[e1][0])swap(e1,e2);

          while(true)
          {
              while(ps[e1][k1]<p && k1<siz[e1])k1++;
              if(k1==siz[e1])break;
              p=ps[e1][k1++];k++;
              while(ps[e2][k2]<p && k2<siz[e2])k2++;
              if(k2==siz[e2])break;
              p=ps[e2][k2++];k++;
          }

          if(k>mx){mx=k;}

      }
    }

    cout<<mx;

return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<cmath>
#include<deque>
#include<queue>

#define nx(x) (x | (x+1))
#define pr(x) (x & (x+1))

#define fi first
#define MAXN 222222
#define se second
#define len(a) (int(a.length()))
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define forit(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

#define pb push_back
#define pf push_front
#define mem(a,b) memset(a,b,sizeof a)
#define eps 1e-8
using namespace std;
typedef long long ll;
map<int,int> m;
int d[4111][4111];
int main()
{
    int i,j,k,a[4444],l=0,n;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
        if(m[a[i]]==0)m[a[i]]=++l;
        a[i]=m[a[i]];
    }

    mem(d,0);
    int mx=0;
    for(i=1;i<n;i++)
    for(j=i+1;j<=n;j++)
    {
        d[a[i]][a[j]]=d[a[j]][a[i]]+1;
        if(d[a[i]][a[j]]>mx)mx=d[a[i]][a[j]];
    }

    cout<<mx+1;

return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<cmath>
#include<deque>
#include<queue>

#define nx(x) (x | (x+1))
#define pr(x) (x & (x+1))

#define fi first
#define MAXN 222222
#define se second
#define len(a) (int(a.length()))
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define forit(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

#define pb push_back
#define pf push_front
#define mem(a,b) memset(a,b,sizeof a)
#define eps 1e-8
using namespace std;
typedef long long ll;
map<int,int> m;
int d[4111][4111];
int main()
{
    int i,j,k,a[4444],l=0,n;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
        if(m[a[i]]==0)m[a[i]]=++l;
        a[i]=m[a[i]];
    }

    mem(d,0);
    int mx=0;
    for(i=1;i<=n;i++)
    for(j=1;j<=l;j++)
    {
        //if(a[i]==a[j])continue;
        d[a[i]][j]=d[j][a[i]]+1;
        if(d[a[i]][j]>mx)mx=d[a[i]][j];
    }

    cout<<mx;

return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<cmath>
#include<deque>
#include<queue>

#define nx(x) (x | (x+1))
#define pr(x) (x & (x+1))

#define fi first
#define MAXN 222222
#define se second
#define sz(a) (int(a.size()))
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define forit(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

#define pb push_back
#define pf push_front
#define mem(a,b) memset(a,b,sizeof a)
#define eps 1e-8
using namespace std;
typedef long long ll;
vector<int> ps[1111111];
int siz[1111111];
map<pair<int,int>,int> was;
int main()
{
    //freopen("input.txt","r",stdin);
    int n,m,a[4444];
    mem(siz,0);
    set<int> s;
    cin>>n;
    for(int i=1;i<=n;i++)
      cin>>a[i],ps[a[i]].pb(i),siz[a[i]]++,s.insert(a[i]);

    set<int>::iterator i,j;

    int mx=1;

    for(i=s.begin();;i++)
    {
      j=i;
      j++;
      if(j==s.end())break;
      for(;j!=s.end();j++)
      {
          int p=1;
          int k=0,k1=0,k2=0;
          int e1=*i,e2=*j;
          if(ps[e2][0]<ps[e1][0])swap(e1,e2);

          while(true)
          {
              while(ps[e1][k1]<p && k1<siz[e1])k1++;
              if(k1==siz[e1])break;
              p=ps[e1][k1++];k++;
              while(ps[e2][k2]<p && k2<siz[e2])k2++;
              if(k2==siz[e2])break;
              p=ps[e2][k2++];k++;
          }

          if(k>mx){mx=k;}

      }
    }

    cout<<mx;

return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<cmath>
#include<deque>
#include<queue>

#define nx(x) (x | (x+1))
#define pr(x) (x & (x+1))

#define fi first
#define MAXN 222222
#define se second
#define len(a) (int(a.length()))
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define forit(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

#define pb push_back
#define pf push_front
#define mem(a,b) memset(a,b,sizeof a)
#define eps 1e-8
using namespace std;
typedef long long ll;
map<int,int> m;
int d[4111][4111];
int main()
{
    int i,j,k,a[4444],l=0,n;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
        if(m[a[i]]==0)m[a[i]]=++l;
        a[i]=m[a[i]];
    }

    mem(d,0);
    int mx=0;
    for(i=1;i<n;i++)
    for(j=i+1;j<=n;j++)
    {
        if(a[i]==a[j])continue;
        d[a[i]][a[j]]=d[a[j]][a[i]]+1;
        if(d[a[i]][a[j]]>mx)mx=d[a[i]][a[j]];
    }

    cout<<mx+1;

return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<cmath>
#include<deque>
#include<queue>

#define nx(x) (x | (x+1))
#define pr(x) (x & (x+1))

#define fi first
#define MAXN 222222
#define se second
#define len(a) (int(a.length()))
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define forit(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

#define pb push_back
#define pf push_front
#define mem(a,b) memset(a,b,sizeof a)
#define eps 1e-8
using namespace std;
typedef long long ll;
map<int,int> m;
int d[4111][4111];
int main()
{
    int i,j,k,a[4444],l=0,n;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
        if(m[a[i]]==0)m[a[i]]=++l;
        a[i]=m[a[i]];
    }

    mem(d,0);
    int mx=0;
    for(i=1;i<n;i++)
    for(j=i+1;j<=n;j++)
    {
        if(a[i]!=a[j])continue;
        d[a[i]][a[j]]=d[a[j]][a[i]]+1;
        if(d[a[i]][a[j]]>mx)mx=d[a[i]][a[j]];
    }

    cout<<mx+1;

return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>

#define pb push_back
#define SIZE(A) ((int)A.size())
using namespace std;

vector<int> ar,srt;

const int MAXN = 4010;

int cnt[MAXN];
int tmp[MAXN];

int main(){

	int n;

	scanf(" %d",&n);

	for(int i=1,a;i<=n;i++)
	{
		scanf(" %d",&a);
		srt.pb(a);
		ar.pb(a);
	}

	sort(srt.begin(),srt.end());
	srt.resize(unique(srt.begin(),srt.end())-srt.begin());

	for(int i=0;i<n;i++)
		ar[i]=lower_bound(srt.begin(),srt.end(),ar[i])-srt.begin();

	int res=0;

	for(int i=0;i<n;i++)
	{
		memset(cnt,0,sizeof cnt);
		memset(tmp,0,sizeof tmp);
		for(int j=i;j<n;j++){
			if(ar[i]==ar[j])
			{
				for(int t=0;t<n;t++)
				{
					if(tmp[t])
						cnt[t]++,cnt[t]++;
					if(j==i)
						cnt[t]++;
					res=max(res,cnt[t]);
				}
				memset(tmp,0,sizeof tmp);
				/*if(i==0)
				{
					for(int t=0;t<n;t++)
						printf("%d ",cnt[t]);
					puts("");
				}*/
			}
			else
				tmp[ar[j]]=1,res=max(res,cnt[ar[j]]+1);
		}
	}
	cout << res << endl;

	return 0;

}


#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>

#define pb push_back
#define SIZE(A) ((int)A.size())
using namespace std;

vector<int> ar,srt;

const int MAXN = 4010;

int cnt[MAXN];
int tmp[MAXN];
int used[MAXN];

int main(){

	int n;

	scanf(" %d",&n);

	for(int i=1,a;i<=n;i++)
	{
		scanf(" %d",&a);
		srt.pb(a);
		ar.pb(a);
	}

	sort(srt.begin(),srt.end());
	srt.resize(unique(srt.begin(),srt.end())-srt.begin());

	for(int i=0;i<n;i++)
		ar[i]=lower_bound(srt.begin(),srt.end(),ar[i])-srt.begin();

	int res=0;

	for(int i=0;i<n;i++)
		if(!used[i])
		{
			memset(cnt,0,sizeof cnt);
			memset(tmp,0,sizeof tmp);
			for(int j=i;j<n;j++){
				if(ar[i]==ar[j])
				{
					used[j]=1;
					for(int t=0;t<SIZE(srt);t++)
					{
						if(tmp[t])
							cnt[t]+=2;
						if(j==i)
							cnt[t]++;
						else if(t==ar[i])
							cnt[t]++;
						res=max(res,cnt[t]);
					}
					memset(tmp,0,sizeof tmp);
					/*if(i==0)
					{
						for(int t=0;t<n;t++)
							printf("%d ",cnt[t]);
						puts("");
					}*/
				}
				else
					tmp[ar[j]]=1,res=max(res,cnt[ar[j]]+1);
			}
		}
	cout << res << endl;

	return 0;

}


#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>

#define pb push_back
#define SIZE(A) ((int)A.size())
using namespace std;

vector<int> ar,srt;

const int MAXN = 4010;

int cnt[MAXN];
int tmp[MAXN];
int used[MAXN];

int main(){

	int n;

	scanf(" %d",&n);

	for(int i=1,a;i<=n;i++)
	{
		scanf(" %d",&a);
		srt.pb(a);
		ar.pb(a);
	}

	sort(srt.begin(),srt.end());
	srt.resize(unique(srt.begin(),srt.end())-srt.begin());

	for(int i=0;i<n;i++)
		ar[i]=lower_bound(srt.begin(),srt.end(),ar[i])-srt.begin();

	int res=0;

	for(int i=0;i<n;i++)
		if(!used[i])
		{
			memset(cnt,0,sizeof cnt);
			memset(tmp,0,sizeof tmp);
			for(int j=i;j<n;j++){
				if(ar[i]==ar[j])
				{
					used[j]=1;
					for(int t=0;t<n;t++)
					{
						if(tmp[t])
							cnt[t]++,cnt[t]++;
						if(j==i)
							cnt[t]++;
						res=max(res,cnt[t]);
					}
					memset(tmp,0,sizeof tmp);
					/*if(i==0)
					{
						for(int t=0;t<n;t++)
							printf("%d ",cnt[t]);
						puts("");
					}*/
				}
				else
					tmp[ar[j]]=1,res=max(res,cnt[ar[j]]+1);
			}
		}
	cout << res << endl;

	return 0;

}


#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>

#define pb push_back
#define SIZE(A) ((int)A.size())
using namespace std;

vector<int> ar,srt;

const int MAXN = 4010;

int cnt[MAXN];
int tmp[MAXN];

int main(){

	int n;

	scanf(" %d",&n);

	for(int i=1,a;i<=n;i++)
	{
		scanf(" %d",&a);
		srt.pb(a);
		ar.pb(a);
	}

	sort(srt.begin(),srt.end());
	srt.resize(unique(srt.begin(),srt.end())-srt.begin());

	for(int i=0;i<n;i++)
		ar[i]=lower_bound(srt.begin(),srt.end(),ar[i])-srt.begin();

	int res=0;

	for(int i=0;i<n;i++)
	{
		memset(cnt,0,sizeof cnt);
		memset(tmp,0,sizeof tmp);
		for(int j=i;j<n;j++){
			if(ar[i]==ar[j])
			{
				for(int t=0;t<n;t++)
				{
					if(tmp[t])
						cnt[t]++;
					else
						cnt[t]=0;
					cnt[t]++;
					res=max(res,cnt[t]);
				}
				memset(tmp,0,sizeof tmp);
			}
			else
				tmp[ar[j]]=1,res=max(res,cnt[ar[j]]+1);
		}
	}
	cout << res << endl;

	return 0;

}


#include <ctime>
#include <numeric>
#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <string>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
#include <sstream>
#include <map>
#include <set>
#include <cstdio>
#include <queue>
using namespace std;
//map<int,int> m[1000002];
int m[4001][4001];
int mapa[1000001];

int main(){

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int c[4002];
    int n;

    while(cin>>n){
        memset(mapa,0,sizeof(mapa));
        memset(m,0,sizeof(m));
        set<int>S;

        for(int i=0;i<n;i++){
            cin>>c[i];
            S.insert(c[i]);
        }

        vector<int>v(S.begin(),S.end());
        for(int i=0;i<v.size();i++)
            mapa[v[i]]=i;

        for(int i=0;i<n;i++)
            c[i]=mapa[c[i]];

        int dev=2;
        for(int i=0;i<n;i++){

            for(int j=0;j<4000;j++){
                m[j][c[i]]= max(m[j][c[i]], m[c[i]][j]+1 );
                dev=max(dev,m[j][c[i]]);
            }

            for(int j=0;j<i;j++)
                m[j][c[i]]=max(m[j][c[i]],2);

        }

        if(n<=2){
            cout<<n<<endl;
        }else{
            cout<<dev<<endl;
        }

    }
    return 0;
}


#include <ctime>
#include <numeric>
#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <string>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
#include <sstream>
#include <map>
#include <set>
#include <cstdio>
#include <queue>
using namespace std;
map<int,int> m[1000002];

int main(){

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int c[4002];
    int n;

    while(cin>>n){
        for(int i=0;i<1000002;i++)m[i].clear();

        int dev=2;
        for(int i=0;i<n;i++){
            cin>>c[i];
            map<int,int>::iterator it=m[c[i]].begin();

            for(;it!=m[c[i]].end();it++){
                m[(*it).first][c[i]]=  max(m[(*it).first][c[i]], (*it).second+1);
                dev=max(dev, m[(*it).first][c[i]]);
            }

            for(int j=0;j<i;j++)
                m[c[j]][c[i]]=max(m[c[j]][c[i]],2);

        }

        if(n<=2){
            cout<<n<<endl;
        }else{
            cout<<dev<<endl;
        }

    }
    return 0;
}


#include <ctime>
#include <numeric>
#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <string>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
#include <sstream>
#include <map>
#include <set>
#include <cstdio>
#include <queue>
using namespace std;
//map<int,int> m[1000002];
int m[4001][4001];
int mapa[1000001];

int main(){

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int c[4002];
    int n;

    while(cin>>n){
        memset(mapa,0,sizeof(mapa));
        memset(m,0,sizeof(m));
        set<int>S;

        for(int i=0;i<n;i++){
            cin>>c[i];
            S.insert(c[i]);
        }

        vector<int>v(S.begin(),S.end());
        for(int i=0;i<v.size();i++)
            mapa[v[i]]=i;

        for(int i=0;i<n;i++)
            c[i]=mapa[c[i]];

        int dev=2;
        for(int i=0;i<n;i++){

            for(int j=0;j<4000;j++){
                m[j][c[i]]= max(m[j][c[i]], m[c[i]][j]+1 );
                dev=max(dev,m[j][c[i]]);
            }

            for(int j=0;j<i;j++)
                m[c[j]][c[i]]=max(m[c[j]][c[i]],2);

        }

        if(n<=2){
            cout<<n<<endl;
        }else{
            cout<<dev<<endl;
        }

    }
    return 0;
}


#include <ctime>
#include <numeric>
#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <string>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
#include <sstream>
#include <map>
#include <set>
#include <cstdio>
#include <queue>
using namespace std;
int c[4001];
int p,q;
int n;
int memo[4001][4001];

int dp(int pos,int ind){
    if(pos>=n)return 0;
    if(memo[pos][ind]!=-1)return memo[pos][ind];
    int dev=0;
    if(c[pos]==c[ind])dev=1+dp(pos+2,ind);
    memo[pos][ind]=dev;
    return dev;
}

int main(){

    //freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

    while(cin>>n){
        memset(memo,-1,sizeof(memo));

        for(int i=0;i<n;i++)cin>>c[i];
        if(n<=2){
            cout<<n<<endl;
            continue;
        }

        int dev=1;
        int val[n];

        for(int i=0;i<n;i++){
            val[i]=dp(i,i);
        }

        for(int i=0;i+1<n;i++){
            int primer=val[i];
            int seg=val[i+1];
            if(primer>seg)
                dev=max(dev,2*seg+1);
            else
                dev=max(dev,2*primer);
        }

        cout<<dev<<endl;
    }

    return 0;
}


#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;

map<int,int> mp;
int a[4005];
int b[4005];
int dp[4005][4005];
int nxt[4005][4005]; // pos, val

int main()
{
    int n, k; cin >> n;
    for (int i=0; i<n; ++i) { cin >> a[i]; b[i] = a[i]; }
    sort(b, b+n);
    k=0;
    for (int i=0; i<n; ++i) if (!mp.count(b[i])) mp[b[i]] = k++;
    for (int i=0; i<n; ++i) a[i] = mp[a[i]];

    memset(nxt, -1, sizeof nxt);
    for (int i=0; i<n; ++i)
    for (int j=i+1; j<n; ++j)
        if (nxt[i][a[j]] < 0) nxt[i][a[j]] = j;

    for (int i=0; i<n; ++i)
    for (int k=0; k<n; ++k)
        dp[i][k] = 1;

    for (int i=n-1; i>=0; --i)
    for (int k=0; k<n; ++k) {
        int j = nxt[i][k];
        if (j>=0)
            dp[j][a[i]] = dp[i][k] + 1;
    }

    int best = 0;
    for (int i=0; i<n; ++i)
    for (int k=0; k<n; ++k)
        best = max(best, dp[i][k]);
    cout << best << endl;
}


#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;

map<int,int> mp;
int a[4005];
int b[4005];
int dp[4005][4005];
int nxt[4005][4005]; // pos, val

int main()
{
    int n, k; cin >> n;
    for (int i=0; i<n; ++i) { cin >> a[i]; b[i] = a[i]; }
    sort(b, b+n);
    k=0;
    for (int i=0; i<n; ++i) if (!mp.count(b[i])) mp[b[i]] = k++;
    for (int i=0; i<n; ++i) a[i] = mp[a[i]];

    memset(nxt, -1, sizeof nxt);
    for (int i=n-1; i>0; --i)
    for (int j=i-1; j>=0; --j)
        if (nxt[i][a[j]] < 0) nxt[i][a[j]] = j;

    for (int i=0; i<n; ++i)
    for (int k=0; k<n; ++k)
        dp[i][k] = 1;

    for (int i=n-1; i>=0; --i)
    for (int k=0; k<n; ++k) {
        int j = nxt[i][k];
        if (j>=0)
            dp[j][a[i]] = dp[i][k] + 1;
    }

    int best = 0;
    for (int i=0; i<n; ++i)
    for (int k=0; k<n; ++k)
        best = max(best, dp[i][k]);
    cout << best << endl;
}


#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;

map<int,int> mp;
int a[4005];
int b[4005];
int dp[4005][4005];
int nxt[4005][4005]; // pos, val

int main()
{
    int n, k; cin >> n;
    for (int i=0; i<n; ++i) { cin >> a[i]; b[i] = a[i]; }
    sort(b, b+n);
    k=0;
    for (int i=0; i<n; ++i) if (!mp.count(b[i])) mp[b[i]] = k++;
    for (int i=0; i<n; ++i) a[i] = mp[a[i]];

    memset(nxt, -1, sizeof nxt);
    for (int i=0; i<n; ++i)
    for (int j=i+1; j<n; ++j)
        if (nxt[i][a[j]] < 0) nxt[i][a[j]] = j;

    for (int i=0; i<n; ++i)
    for (int k=0; k<n; ++k)
        dp[i][k] = 1;

    for (int i=0; i<n; ++i)
    for (int k=0; k<n; ++k) {
        int j = nxt[i][k];
        if (j>=0)
            dp[j][a[i]] = max(dp[j][a[i]], dp[i][k] + 1);
    }

    int best = 0;
    for (int i=0; i<n; ++i)
    for (int k=0; k<n; ++k)
        best = max(best, dp[i][k]);
    cout << best << endl;
}


#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;

map<int,int> mp;
int a[4005];
int b[4005];
int dp[4005][4005];
int nxt[4005][4005]; // pos, val

int main()
{
    int n, k; cin >> n;
    for (int i=0; i<n; ++i) { cin >> a[i]; b[i] = a[i]; }
    sort(b, b+n);
    k=0;
    for (int i=0; i<n; ++i) if (!mp.count(b[i])) mp[b[i]] = k++;
    for (int i=0; i<n; ++i) a[i] = mp[a[i]];

    memset(nxt, -1, sizeof nxt);
    for (int i=0; i<n; ++i)
    for (int j=i+1; j<n; ++j)
        if (nxt[i][a[j]] < 0) nxt[i][a[j]] = j;

    for (int i=0; i<n; ++i)
    for (int k=0; k<n; ++k)
        dp[i][k] = 1;

    for (int i=n-1; i>=0; ++i)
    for (int k=0; k<n; ++k) {
        int j = nxt[i][k];
        if (j>=0)
            dp[j][a[i]] = dp[i][k] + 1;
    }

    int best = 0;
    for (int i=0; i<n; ++i)
    for (int k=0; k<n; ++k)
        best = max(best, dp[i][k]);
    cout << best << endl;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 4010;

int N, arr[MAXN];
map<int, int> mp;
vector<int> occ[MAXN];
int ans;

void setans (int x) {
	if (ans < x) {
		ans = x;
	}
}

int seq (int a, int b) {
	// cerr << a << ' ' << b << endl;
	vector<int>::iterator it1 = occ[a].begin(), it2 = occ[b].begin();
	int ans = 2;
	while (it1 != occ[a].end() && it2 != occ[b].end()) {
		//find it2 after it1
		if ((*it1) < (*it2)) {
			// cerr << "ha\n";
			//find it1 after it2
			while (it1 != occ[a].end() && (*it1) < (*it2)) {
				it1++;
			}
			if (it1 == occ[a].end()) {
				break;
			} else {
				ans++;
			}
		} else {
			while (it2 != occ[b].end() && (*it2) < (*it1)) {
				it2++;
			}
			if (it2 == occ[b].end()) {
				break;
			} else {
				ans++;
			}
		}
	}
	// cerr << "returned " << ans << " for " << a << ' ' << b << endl;
	return ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("codeforces256A.in", "r", stdin);
#endif
	cin >> N;
	if (N == 1) {
		cout << "1\n";
		return 0;
	}
	for (int i = 1; i <= N; i++) {
		cin >> arr[i];
		mp[arr[i]];
	}
	map<int, int>::iterator it = mp.begin();
	for (int i = 1; it != mp.end(); i++, it++) {
		it->second = i;
	}
	for (int i = 1; i <= N; i++) {
		int& a = arr[i];
		a = mp[a];
		occ[a].push_back(i);
		// cerr << "pb " << a << endl;
	}
	for (int i = 1; i < MAXN; i++) {
		setans(occ[i].size());
	}
	for (int i = 1; i < MAXN; i++) {
		for (int j = 1; j < MAXN; j++) {
			//do a sweep line between i and j
			if (i != j && !occ[i].empty() && !occ[j].empty())
				setans(seq(i, j));
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 4010;

int N, arr[MAXN];
map<int, int> mp;
vector<int> occ[MAXN];
int ans;

void setans (int x) {
	if (ans < x) {
		ans = x;
	}
}

int seq (int a, int b) {
	// cerr << a << ' ' << b << endl;
	vector<int>::iterator it1 = occ[a].begin(), it2 = occ[b].begin();
	int ans = 2;
	while (it1 != occ[a].end() && it2 != occ[b].end()) {
		//find it2 after it1
		if ((*it1) < (*it2)) {
			// cerr << "ha\n";
			//find it1 after it2
			while (it1 != occ[a].end() && (*it1) < (*it2)) {
				it1++;
			}
			if (it1 == occ[a].end()) {
				break;
			} else {
				ans++;
			}
		} else {
			while (it2 != occ[b].end() && (*it2) < (*it1)) {
				it2++;
			}
			if (it2 == occ[b].end()) {
				break;
			} else {
				ans++;
			}
		}
	}
	// cerr << "returned " << ans << " for " << a << ' ' << b << endl;
	return ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("codeforces256A.in", "r", stdin);
#endif
	cin >> N;
	if (N == 1) {
		cout << "1\n";
		return 0;
	}
	for (int i = 1; i <= N; i++) {
		cin >> arr[i];
		mp[arr[i]];
	}
	map<int, int>::iterator it = mp.begin();
	for (int i = 1; it != mp.end(); i++, it++) {
		it->second = i;
	}
	for (int i = 1; i <= N; i++) {
		int& a = arr[i];
		a = mp[a];
		occ[a].push_back(i);
		// cerr << "pb " << a << endl;
	}
	for (int i = 1; i < MAXN; i++) {
		for (int j = 1; j < MAXN; j++) {
			//do a sweep line between i and j
			if (!occ[i].empty() && !occ[j].empty())
				setans(seq(i, j));
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 4010;

int N, arr[MAXN];
map<int, int> mp;
vector<int> occ[MAXN];
int ans;

void setans (int x) {
	if (ans < x) {
		ans = x;
	}
}

int seq (int a, int b) {
	// cerr << a << ' ' << b << endl;
	vector<int>::iterator it1 = occ[a].begin(), it2 = occ[b].begin();
	int ans = 2;
	while (it1 != occ[a].end() && it2 != occ[b].end()) {
		//find it2 after it1
		if ((*it1) < (*it2)) {
			// cerr << "ha\n";
			//find it1 after it2
			while (it1 != occ[a].end() && (*it1) < (*it2)) {
				it1++;
			}
			if (it1 == occ[a].end()) {
				break;
			} else {
				ans++;
			}
		} else {
			while (it2 != occ[b].end() && (*it2) < (*it1)) {
				it2++;
			}
			if (it2 == occ[b].end()) {
				break;
			} else {
				ans++;
			}
		}
	}
	// cerr << "returned " << ans << " for " << a << ' ' << b << endl;
	return ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("codeforces256A.in", "r", stdin);
#endif
	cin >> N;
	for (int i = 1; i <= N; i++) {
		cin >> arr[i];
		mp[arr[i]];
	}
	map<int, int>::iterator it = mp.begin();
	for (int i = 1; it != mp.end(); i++, it++) {
		it->second = i;
	}
	for (int i = 1; i <= N; i++) {
		int& a = arr[i];
		a = mp[a];
		occ[a].push_back(i);
		// cerr << "pb " << a << endl;
	}
	for (int i = 1; i < MAXN; i++) {
		for (int j = 1; j < MAXN; j++) {
			//do a sweep line between i and j
			if (i != j && !occ[i].empty() && !occ[j].empty())
				setans(seq(i, j));
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 4010;

int N, arr[MAXN];
map<int, int> mp;
vector<int> occ[MAXN];
int ans;

void setans (int x) {
	if (ans < x) {
		ans = x;
	}
}

int seq (int a, int b) {
	// cerr << a << ' ' << b << endl;
	vector<int>::iterator it1 = occ[a].begin(), it2 = occ[b].begin();
	int ans = 2;
	while (it1 != occ[a].end() && it2 != occ[b].end()) {
		//find it2 after it1
		if ((*it1) < (*it2)) {
			// cerr << "ha\n";
			//find it1 after it2
			while (it1 != occ[a].end() && (*it1) < (*it2)) {
				it1++;
			}
			if (it1 == occ[a].end()) {
				break;
			} else {
				ans++;
			}
		} else {
			while (it2 != occ[b].end() && (*it2) < (*it1)) {
				it2++;
			}
			if (it2 == occ[b].end()) {
				break;
			} else {
				ans++;
			}
		}
	}
	// cerr << "returned " << ans << " for " << a << ' ' << b << endl;
	return ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("codeforces256A.in", "r", stdin);
#endif
	cin >> N;
	if (N == 1) {
		cout << "1\n";
		return 0;
	}
	for (int i = 1; i <= N; i++) {
		cin >> arr[i];
		mp[arr[i]];
	}
	map<int, int>::iterator it = mp.begin();
	for (int i = 1; it != mp.end(); i++, it++) {
		it->second = i;
	}
	for (int i = 1; i <= N; i++) {
		int& a = arr[i];
		a = mp[a];
		occ[a].push_back(i);
		// cerr << "pb " << a << endl;
	}
	for (int i = 1; i < MAXN; i++) {
		for (int j = 1; j < MAXN; j++) {
			//do a sweep line between i and j
			if (i != j && !occ[i].empty() && !occ[j].empty())
				setans(seq(i, j));
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <set>

using namespace std;
int n, b[4444], t, last[4444], a[4444][4444], c[4444], m;
map<int, int> k;
int main(){
	cin >> n;
	for (int i=1; i<=n; i++) {
		cin >> b[i];
		if (!k[b[i]]) k[b[i]]=i;
		b[i]=k[b[i]];
	}
	for (int i=1; i<=n; i++) {
		set<int> s;
		for (int j=last[b[i]]+1; j<i; j++) s.insert(b[j]);
		for (set<int>::iterator j=s.begin(); j!=s.end(); j++) a[b[i]][*j]+=2;
		last[b[i]]=i;
		c[b[i]]++;
	}
	for (int i=1; i<=n; i++) {
		set<int> s;
		for (int j=last[i]+1; j<=n; j++) s.insert(b[j]);
		for (set<int>::iterator j=s.begin(); j!=s.end(); j++) a[i][*j]++;
	}
	for (int i=1; i<=n; i++) if (c[i]>m) m=c[i];
	for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if (i!=j&&a[i][j]>m) m=a[i][j];
	cout << m;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <set>

using namespace std;
int n, b[4444], t, last[4444], a[4444][4444], c[4444], m;
map<int, int> k;
int main(){
	cin >> n;
	for (int i=1; i<=n; i++) {
		cin >> b[i];
		if (!k[b[i]]) k[b[i]]=i;
		b[i]=k[b[i]];
	}
	for (int i=1; i<=n; i++) {
		set<int> s;
		for (int j=last[b[i]]+1; j<i; j++) s.insert(b[j]);
		for (set<int>::iterator j=s.begin(); j!=s.end(); j++) a[b[i]][*j]+=2;
		last[b[i]]=i;
		c[b[i]]++;
	}
	for (int i=1; i<=n; i++) if (last[i]!=0) {
		set<int> s;
		for (int j=last[i]+1; j<=n; j++) s.insert(b[j]);
		for (set<int>::iterator j=s.begin(); j!=s.end(); j++) a[i][*j]++;
	}
	for (int i=1; i<=n; i++) if (c[i]>m) m=c[i];
	for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if (i!=j&&a[i][j]>m) m=a[i][j];
	cout << m;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <set>

using namespace std;
int n, b[4444], t, last[4444], a[4444][4444], c[4444], m, d[4444];
map<int, int> k;
int main(){
	cin >> n;
	for (int i=1; i<=n; i++) {
		cin >> b[i];
		if (!k[b[i]]) k[b[i]]=i;
		b[i]=k[b[i]];
	}
	for (int i=1; i<=n; i++) {
		for (int j=last[b[i]]+1; j<i; j++) if (!d[b[j]]) d[b[j]]=1, a[i][b[j]]++;
		for (int j=last[b[i]]+1; j<i; j++) d[b[j]]=1;
		last[b[i]]=i;
		c[b[i]]++;
	}
	for (int i=1; i<=n; i++) {
		set<int> s;
		for (int j=last[i]+1; j<=n; j++) s.insert(b[j]);
		for (set<int>::iterator j=s.begin(); j!=s.end(); j++) a[i][*j]++;
	}
	for (int i=1; i<=n; i++) if (c[i]>m) m=c[i];
	for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if (i!=j&&a[i][j]>m) m=a[i][j];
	cout << m;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <set>

using namespace std;
int n, b[4444], t, last[4444], a[4444][4444], c[4444], m, d[4444];
map<int, int> k;
int main(){
	cin >> n;
	for (int i=1; i<=n; i++) {
		cin >> b[i];
		if (!k[b[i]]) k[b[i]]=i;
		b[i]=k[b[i]];
	}
	for (int i=1; i<=n; i++) {
		for (int j=last[b[i]]+1; j<i; j++) if (!d[b[j]]) d[b[j]]=1, a[i][b[j]]+=2;
		for (int j=last[b[i]]+1; j<i; j++) d[b[j]]=0;
		last[b[i]]=i;
		c[b[i]]++;
	}
	for (int i=1; i<=n; i++) {
		set<int> s;
		for (int j=last[i]+1; j<=n; j++) s.insert(b[j]);
		for (set<int>::iterator j=s.begin(); j!=s.end(); j++) a[i][*j]++;
	}
	for (int i=1; i<=n; i++) if (c[i]>m) m=c[i];
	for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if (i!=j&&a[i][j]>m) m=a[i][j];
	cout << m;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <set>

using namespace std;
int n, b[4444], t, last[4444], a[4444][4444], c[4444], m, d[4444];
map<int, int> k;
int main(){
	cin >> n;
	for (int i=1; i<=n; i++) {
		cin >> b[i];
		if (!k[b[i]]) k[b[i]]=i;
		b[i]=k[b[i]];
	}
	for (int i=1; i<=n; i++) {
		for (int j=last[b[i]]+1; j<i; j++) if (!d[b[j]]) d[b[j]]=1, a[i][b[j]]++;
		for (int j=last[b[i]]+1; j<i; j++) d[b[j]]=0;
		last[b[i]]=i;
		c[b[i]]++;
	}
	for (int i=1; i<=n; i++) {
		set<int> s;
		for (int j=last[i]+1; j<=n; j++) s.insert(b[j]);
		for (set<int>::iterator j=s.begin(); j!=s.end(); j++) a[i][*j]++;
	}
	for (int i=1; i<=n; i++) if (c[i]>m) m=c[i];
	for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if (i!=j&&a[i][j]>m) m=a[i][j];
	cout << m;
    return 0;
}


/*
  ------------------------- Hachiikung ---------------------------------
  ---------------------- Worrachate Bosri ------------------------------
  ------ Faculty of Computer Engineering Chulalongkorn University ------
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<utility>
#include<sstream>
using namespace std;
#define REP(i,FN) for(int i=0;i<FN;i++)
#define FOR(i,ST,FN) for(int i=ST;i<=FN;i++)
#define FORD(i,FN,ST) for(int i=FN;i>=ST;i--)
#define FORX(x) for(it=x.begin();it!=x.end();it++)
#define pause system("pause")
#define S scanf
#define P printf
#define X first
#define Y second
#define pb push_back
#define PII pair<int,int>

const int MOD(1000000007);
const int INF((1<<30)-1);
const int MAXN(4004);

int a[MAXN];

map<int,int>d;

int main(){

    int n;
    S("%d",&n);
    REP(i,n)
     S("%d",&a[i]);

    int MAX=0;

    REP(i,n)
    {
     int j=i+1;

     d.clear();

     int ans=1,tmp=0;

     while(1)
     {
      while( a[j]!=a[i] && j<n )
      {
       d[a[j]]++;
       tmp=max(tmp,d[a[j]]);
       j++;
      }

      ans=max(ans,2*tmp);

      if(j==n) break;

      ans=max(ans,2*tmp+1);

      j++;
     }

     MAX=max(MAX,ans);
    }

    P("%d",MAX);
}


/*
  ------------------------- Hachiikung ---------------------------------
  ---------------------- Worrachate Bosri ------------------------------
  ------ Faculty of Computer Engineering Chulalongkorn University ------
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<utility>
#include<sstream>
using namespace std;
#define REP(i,FN) for(int i=0;i<FN;i++)
#define FOR(i,ST,FN) for(int i=ST;i<=FN;i++)
#define FORD(i,FN,ST) for(int i=FN;i>=ST;i--)
#define FORX(x) for(it=x.begin();it!=x.end();it++)
#define pause system("pause")
#define S scanf
#define P printf
#define X first
#define Y second
#define pb push_back
#define PII pair<int,int>

const int MOD(1000000007);
const int INF((1<<30)-1);
const int MAXN(4004);
const int MAXK(1000001)

int a[MAXN],b[MAXK]={0};

map<int,int>d,b;

int main(){

    int n;
    S("%d",&n);
    REP(i,n)
     S("%d",&a[i]);

    int MAX=0,k=1;

    REP(i,n)
    {
     int j=i+1;

     d.clear();

     int ans=1,tmp=0,cnt=1;

     while(1)
     {
      while( a[j]!=a[i] && j<n )
      {
       if(b[a[j]]!=k)
       {
        b[a[j]]=k;
        d[a[j]]++;
        tmp=max(tmp,d[a[j]]);
       }
       j++;
      }

      ans=max(ans,2*tmp);

      if(j==n) break;

      ans=max(ans,2*tmp+1);

      cnt++,j++,k++;
     }

     ans=max(ans,cnt);
     MAX=max(MAX,ans);
    }

    P("%d",MAX);
}


/*
  ------------------------- Hachiikung ---------------------------------
  ---------------------- Worrachate Bosri ------------------------------
  ------ Faculty of Computer Engineering Chulalongkorn University ------
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<utility>
#include<sstream>
using namespace std;
#define REP(i,FN) for(int i=0;i<FN;i++)
#define FOR(i,ST,FN) for(int i=ST;i<=FN;i++)
#define FORD(i,FN,ST) for(int i=FN;i>=ST;i--)
#define FORX(x) for(it=x.begin();it!=x.end();it++)
#define pause system("pause")
#define S scanf
#define P printf
#define X first
#define Y second
#define pb push_back
#define PII pair<int,int>

const int MOD(1000000007);
const int INF((1<<30)-1);
const int MAXN(4004);

int a[MAXN];

map<int,int>d,b;

int main(){

    int n;
    S("%d",&n);
    REP(i,n)
     S("%d",&a[i]);

    int MAX=0;

    REP(i,n)
    {
     int j=i+1;

     d.clear(); b.clear();

     int ans=1,tmp=0,cnt=1,k=1;

     while(1)
     {
      while( a[j]!=a[i] && j<n )
      {
       if(b[a[j]]!=k)
       {
        b[a[j]]=k;
        d[a[j]]++;
        tmp=max(tmp,d[a[j]]);
       }
       j++;
      }

      ans=max(ans,2*tmp);

      if(j==n) break;

      ans=max(ans,2*tmp+1);

      cnt++,j++,k++;
     }

     ans=max(ans,cnt);
     MAX=max(MAX,ans);
    }

    P("%d",MAX);
}


/*
  ------------------------- Hachiikung ---------------------------------
  ---------------------- Worrachate Bosri ------------------------------
  ------ Faculty of Computer Engineering Chulalongkorn University ------
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<utility>
#include<sstream>
using namespace std;
#define REP(i,FN) for(int i=0;i<FN;i++)
#define FOR(i,ST,FN) for(int i=ST;i<=FN;i++)
#define FORD(i,FN,ST) for(int i=FN;i>=ST;i--)
#define FORX(x) for(it=x.begin();it!=x.end();it++)
#define pause system("pause")
#define S scanf
#define P printf
#define X first
#define Y second
#define pb push_back
#define PII pair<int,int>

const int MOD(1000000007);
const int INF((1<<30)-1);
const int MAXN(4004);

int a[MAXN];

map<int,int>d;

int main(){

    int n;
    S("%d",&n);
    REP(i,n)
     S("%d",&a[i]);

    int MAX=0;

    REP(i,n)
    {
     int j=i+1;

     d.clear();

     int ans=1,ans2=0;

     while(1)
     {
      while( a[j]!=a[i] && j<n )
      {
       d[a[j]]++;
       ans2=max(ans2,d[a[j]]);
       j++;
      }

      if(j==n) break;

      ans++,j++;
     }

     MAX=max(MAX,ans+ans2);
    }

    P("%d",MAX);
}


/*
  ------------------------- Hachiikung ---------------------------------
  ---------------------- Worrachate Bosri ------------------------------
  ------ Faculty of Computer Engineering Chulalongkorn University ------
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<utility>
#include<sstream>
using namespace std;
#define REP(i,FN) for(int i=0;i<FN;i++)
#define FOR(i,ST,FN) for(int i=ST;i<=FN;i++)
#define FORD(i,FN,ST) for(int i=FN;i>=ST;i--)
#define FORX(x) for(it=x.begin();it!=x.end();it++)
#define pause system("pause")
#define S scanf
#define P printf
#define X first
#define Y second
#define pb push_back
#define PII pair<int,int>

const int MOD(1000000007);
const int INF((1<<30)-1);
const int MAXN(4004);
const int MAXK(1000001);

int a[MAXN],b[MAXK]={0};

map<int,int>d;

int main(){

    int n;
    S("%d",&n);
    REP(i,n)
     S("%d",&a[i]);

    int MAX=0,k=1;

    REP(i,n)
    {
     int j=i+1;

     d.clear();

     int ans=1,tmp=0,cnt=1;

     while(1)
     {
      while( a[j]!=a[i] && j<n )
      {
       if(b[a[j]]!=k)
       {
        b[a[j]]=k;
        d[a[j]]++;
        tmp=max(tmp,d[a[j]]);
       }
       j++;
      }

      ans=max(ans,2*tmp);

      k++;

      if(j==n) break;

      ans=max(ans,2*tmp+1);

      cnt++,j++;
     }

     ans=max(ans,cnt);
     MAX=max(MAX,ans);
    }

    P("%d",MAX);
}


/*
  ------------------------- Hachiikung ---------------------------------
  ---------------------- Worrachate Bosri ------------------------------
  ------ Faculty of Computer Engineering Chulalongkorn University ------
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<utility>
#include<sstream>
using namespace std;
#define REP(i,FN) for(int i=0;i<FN;i++)
#define FOR(i,ST,FN) for(int i=ST;i<=FN;i++)
#define FORD(i,FN,ST) for(int i=FN;i>=ST;i--)
#define FORX(x) for(it=x.begin();it!=x.end();it++)
#define pause system("pause")
#define S scanf
#define P printf
#define X first
#define Y second
#define pb push_back
#define PII pair<int,int>

const int MOD(1000000007);
const int INF((1<<30)-1);
const int MAXN(4004);
const int MAXK(1000001);

int a[MAXN],b[MAXK]={0},p[MAXN],np=0,d[MAXK]={0};

bool chk[MAXK]={0};

int main(){

    int n;
    S("%d",&n);
    REP(i,n)
    {
     S("%d",&a[i]);
     if(!chk[a[i]])
      chk[a[i]]=1,p[np++]=a[i];
    }

    int MAX=0,k=1;

    REP(i,n)
    {
     int j=i+1;

     REP(j,np)
      d[p[j]]=0;

     int ans=1,tmp=0,cnt=1;

     while(1)
     {
      while( a[j]!=a[i] && j<n )
      {
       if(b[a[j]]!=k)
       {
        b[a[j]]=k;
        d[a[j]]++;
        tmp=max(tmp,d[a[j]]);
       }
       j++;
      }

      ans=max(ans,2*tmp);

      k++;

      if(j==n) break;

      ans=max(ans,2*tmp+1);

      cnt++,j++;
     }

     ans=max(ans,cnt);
     MAX=max(MAX,ans);
    }

    P("%d",MAX);
}


/*
  ------------------------- Hachiikung ---------------------------------
  ---------------------- Worrachate Bosri ------------------------------
  ------ Faculty of Computer Engineering Chulalongkorn University ------
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<utility>
#include<sstream>
using namespace std;
#define REP(i,FN) for(int i=0;i<FN;i++)
#define FOR(i,ST,FN) for(int i=ST;i<=FN;i++)
#define FORD(i,FN,ST) for(int i=FN;i>=ST;i--)
#define FORX(x) for(it=x.begin();it!=x.end();it++)
#define pause system("pause")
#define S scanf
#define P printf
#define X first
#define Y second
#define pb push_back
#define PII pair<int,int>

const int MOD(1000000007);
const int INF((1<<30)-1);
const int MAXN(4004);

int a[MAXN];

map<int,int>d,b;

int main(){

    int n;
    S("%d",&n);
    REP(i,n)
     S("%d",&a[i]);

    int MAX=0;

    REP(i,n)
    {
     int j=i+1;

     d.clear();

     int ans=1,tmp=0,cnt=1;

     while(1)
     {
      b.clear();

      while( a[j]!=a[i] && j<n )
      {
       if(!b[a[j]])
       {
        b[a[j]]=1;
        d[a[j]]++;
        tmp=max(tmp,d[a[j]]);
       }
       j++;
      }

      ans=max(ans,2*tmp);

      if(j==n) break;

      ans=max(ans,2*tmp+1);

      cnt++,j++;
     }

     ans=max(ans,cnt);
     MAX=max(MAX,ans);
    }

    P("%d",MAX);
}


/*
  ------------------------- Hachiikung ---------------------------------
  ---------------------- Worrachate Bosri ------------------------------
  ------ Faculty of Computer Engineering Chulalongkorn University ------
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<utility>
#include<sstream>
using namespace std;
#define REP(i,FN) for(int i=0;i<FN;i++)
#define FOR(i,ST,FN) for(int i=ST;i<=FN;i++)
#define FORD(i,FN,ST) for(int i=FN;i>=ST;i--)
#define FORX(x) for(it=x.begin();it!=x.end();it++)
#define pause system("pause")
#define S scanf
#define P printf
#define X first
#define Y second
#define pb push_back
#define PII pair<int,int>

const int MOD(1000000007);
const int INF((1<<30)-1);
const int MAXN(4004);
const int MAXK(1000001);

int a[MAXN],b[MAXK]={0};

map<int,int>d;

int main(){

    int n;
    S("%d",&n);
    REP(i,n)
     S("%d",&a[i]);

    int MAX=0,k=1;

    REP(i,n)
    {
     int j=i+1;

     d.clear();

     int ans=1,tmp=0,cnt=1;

     while(1)
     {
      while( a[j]!=a[i] && j<n )
      {
       if(b[a[j]]!=k)
       {
        b[a[j]]=k;
        d[a[j]]++;
        tmp=max(tmp,d[a[j]]);
       }
       j++;
      }

      ans=max(ans,2*tmp);

      if(j==n) break;

      ans=max(ans,2*tmp+1);

      cnt++,j++,k++;
     }

     ans=max(ans,cnt);
     MAX=max(MAX,ans);
    }

    P("%d",MAX);
}


/*
  ------------------------- Hachiikung ---------------------------------
  ---------------------- Worrachate Bosri ------------------------------
  ------ Faculty of Computer Engineering Chulalongkorn University ------
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<utility>
#include<sstream>
using namespace std;
#define REP(i,FN) for(int i=0;i<FN;i++)
#define FOR(i,ST,FN) for(int i=ST;i<=FN;i++)
#define FORD(i,FN,ST) for(int i=FN;i>=ST;i--)
#define FORX(x) for(it=x.begin();it!=x.end();it++)
#define pause system("pause")
#define S scanf
#define P printf
#define X first
#define Y second
#define pb push_back
#define PII pair<int,int>

const int MOD(1000000007);
const int INF((1<<30)-1);
const int MAXN(4004);
const int MAXK(1000001);

int a[MAXN],b[MAXK]={0},p[MAXN],np=0,d[MAXK]={0};

bool chk[MAXK]={0};

int main(){

    int n;
    S("%d",&n);
    REP(i,n)
    {
     S("%d",&a[i]);
     if(!chk[a[i]])
      chk[a[i]]=1,p[np++]=a[i];
    }

    int MAX=0,k=1;

    REP(i,n)
    {
     int j=i+1;

     REP(j,np)
      d[p[i]]=0;

     int ans=1,tmp=0,cnt=1;

     while(1)
     {
      while( a[j]!=a[i] && j<n )
      {
       if(b[a[j]]!=k)
       {
        b[a[j]]=k;
        d[a[j]]++;
        tmp=max(tmp,d[a[j]]);
       }
       j++;
      }

      ans=max(ans,2*tmp);

      k++;

      if(j==n) break;

      ans=max(ans,2*tmp+1);

      cnt++,j++;
     }

     ans=max(ans,cnt);
     MAX=max(MAX,ans);
    }

    P("%d",MAX);
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
int dp[4010][4010],arr[4010],H[4010],vis[4010],n,m;
void init()
{
    memset(vis,0,sizeof vis);
    sort(H,H+m);
    m=unique(H,H+m)-H;
}
int Hash(int x)
{
    return lower_bound(H,H+m,x)-H;
}
int main()
{
    int i,j,ans;
    while(~scanf("%d",&n))
    {
        if(n==1)
        {
            scanf("%d",&arr[1]);
            printf("1\n");
            continue;
        }
        for(i=0;i<n;i++)
            scanf("%d",&arr[i]),H[i]=arr[i];
        m=n;
        init();
        for(i=0;i<n;i++)
            arr[i]=Hash(arr[i]),vis[arr[i]]++;
        ans=2;
        memset(dp,0,sizeof dp);
        for(i=0;i<n;i++)
        {
            vis[arr[i]]--;
            for(j=i-1;j>=0;j--)
            {
                if(arr[i]==arr[j])
                    continue;
                if(!dp[arr[i]][arr[j]])
                    dp[arr[i]][arr[j]]=i*maxn+2;
                else if(j>dp[arr[i]][arr[j]]/maxn)
                    dp[arr[i]][arr[j]]=i*maxn+dp[arr[i]][arr[j]]+2;
                if(vis[arr[j]])
                    ans=max(ans,dp[arr[i]][arr[j]]%maxn+1);
                else
                    ans=max(ans,dp[arr[i]][arr[j]]%maxn);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
int dp[4010][4010],arr[4010],H[4010],vis[4010],n,m;
clock_t st,ed;
void init()
{
    sort(H,H+m);
    m=unique(H,H+m)-H;
}
int Hash(int x)
{
    return lower_bound(H,H+m,x)-H;
}
int main()
{
    int i,j,ans;
    //freopen("in.txt","r",stdin);
    while(~scanf("%d",&n))
    {
        if(n==1)
        {
            scanf("%d",&arr[1]);
            printf("1\n");
            continue;
        }
        for(i=0;i<n;i++)
            scanf("%d",&arr[i]),H[i]=arr[i];
        //st=clock();
        m=n;
        init();
        for(i=0;i<n;i++)
            arr[i]=Hash(arr[i]);
        ans=2;
        memset(dp,0,sizeof dp);
        memset(vis,0,sizeof vis);
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
                vis[arr[j]]++;
            for(j=i+1;j<n;j++)
            {
                vis[arr[j]]--;
                if(!dp[arr[i]][arr[j]])
                {
                    dp[arr[i]][arr[j]]=j*maxn+2;
                    if(vis[arr[i]])
                        ans=max(3,ans);
                }
                else if(i>dp[arr[i]][arr[j]]/maxn)
                {
                    dp[arr[i]][arr[j]]=j*maxn+dp[arr[i]][arr[j]]%maxn+2;
                    if(vis[arr[i]])
                        ans=max(dp[arr[i]][arr[j]]%maxn+1,ans);
                    ans=max(ans,dp[arr[i]][arr[j]]%maxn);
                }
            }
        }
        //ed=clock();
        printf("%d\n",ans);
        //printf("%lf\n",(double)(ed-st)/CLOCKS_PER_SEC);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
int dp[4010][4010],arr[4010],H[4010],vis[4010],n,m;
void init()
{
    memset(vis,0,sizeof vis);
    sort(H,H+m);
    m=unique(H,H+m)-H;
}
int Hash(int x)
{
    return lower_bound(H,H+m,x)-H;
}
int main()
{
    int i,j,ans;
    while(~scanf("%d",&n))
    {
        if(n==1)
        {
            printf("1\n");
            continue;
        }
        for(i=0;i<n;i++)
            scanf("%d",&arr[i]),H[i]=arr[i];
        m=n;
        init();
        for(i=0;i<n;i++)
            arr[i]=Hash(arr[i]),vis[arr[i]]++;
        ans=2;
        memset(dp,0,sizeof dp);
        for(i=0;i<n;i++)
        {
            vis[arr[i]]--;
            for(j=i-1;j>=0;j--)
            {
                if(!dp[arr[i]][arr[j]])
                    dp[arr[i]][arr[j]]=i*maxn+2;
                else if(j>dp[arr[i]][arr[j]]/maxn)
                    dp[arr[i]][arr[j]]=i*maxn+dp[arr[i]][arr[j]]+2;
                if(vis[arr[j]])
                    ans=max(ans,dp[arr[i]][arr[j]]%maxn+1);
                else
                    ans=max(ans,dp[arr[i]][arr[j]]%maxn);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
int dp[4010][4010],arr[4010],H[4010],vis[4010],n,m;
void init()
{
    memset(vis,0,sizeof vis);
    sort(H,H+m);
    m=unique(H,H+m)-H;
}
int Hash(int x)
{
    return lower_bound(H,H+m,x)-H;
}
int main()
{
    int i,j,ans;
    while(~scanf("%d",&n))
    {
        if(n==1)
        {
            scanf("%d",&arr[1]);
            printf("1\n");
            continue;
        }
        for(i=0;i<n;i++)
            scanf("%d",&arr[i]),H[i]=arr[i];
        m=n;
        init();
        for(i=0;i<n;i++)
            arr[i]=Hash(arr[i]),vis[arr[i]]++;
        ans=2;
        memset(dp,0,sizeof dp);
        for(i=0;i<n;i++)
        {
            vis[arr[i]]--;
            for(j=i-1;j>=0;j--)
            {
                if(!dp[arr[i]][arr[j]])
                    dp[arr[i]][arr[j]]=i*maxn+2;
                else if(j>dp[arr[i]][arr[j]]/maxn)
                    dp[arr[i]][arr[j]]=i*maxn+dp[arr[i]][arr[j]]+2;
                if(vis[arr[j]])
                    ans=max(ans,dp[arr[i]][arr[j]]%maxn+1);
                else
                    ans=max(ans,dp[arr[i]][arr[j]]%maxn);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}