#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define fr(i,a,b) for(int i = a; i <  b; ++i)
#define rep(i,n) fr(i,0,n)
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define N 4010

int pd[N][N][2];

int input[N];
int n;

vector<int> pos[1000010];

int geta(int j, int val, int p, int k){
    int nt = val + (k ? 1 : -1) * p;
    if(nt > 1000000 || nt < 0) return 1;

    vector<int>::iterator qual = lower_bound(pos[nt].begin(), pos[nt].end(), j+1);
    int ind = -1;
    if(qual != pos[nt].end()){
        ind = *qual;
    }

//  if(i == 2 && j == 3 && k == 0) printf(">>> %d %d|%d| > %d\n", nt, qual, n, input[j+1]);
//  if(i == 1 && j == 2 && p == 10) printf(">>> %d %d\n", nt, qual);
    if(ind != -1)
        return pd[j][ind][k];
    else
        return 1;
}

int best;
//int mk[1000100][2];
void faz(){
    best = 1;
    int it = 1;

    for(int i = n - 2; i >= 0; --i){
        for(int j = i + 1; j < n; ++j){
            rep(k, 2){
                int fat = k ? 1 : -1, p = (input[j] - input[i]) / fat;

//              if(mk[input[j]][k] == it) continue;
//              mk[input[j]][k] = it;
                int val = geta(j, input[j], p, 1-k);
                pd[i][j][k] = 1 + val;
                if(!k) best = max(best, pd[i][j][k]);
            }
        }
//      ++it;
    }
}

int main(){
    scanf("%d", &n);
    rep(i, n) scanf("%d", input + i), pos[input[i]].pb(i);

    faz();
    printf("%d\n", best);
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define fr(i,a,b) for(int i = a; i <  b; ++i)
#define rep(i,n) fr(i,0,n)
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define N 4010

int pd[N][N][2];

int input[N];
int n;

int pos[1000010];

int geta(int j, int val, int p, int k){
    int nt = val + (k ? 1 : -1) * p;
    if(nt > 1000000 || nt < 0) return 1;
    int ind = pos[nt];

//  printf("> %d %d %d\n", j, nt, ind);
    if(ind > j)
        return pd[j][ind][k];
    else
        return 1;
}

int best;
//int mk[1000100][2];
void faz(){
    best = 1;
    int it = 1;

    for(int i = n - 2; i >= 0; --i){
        memset(pos, -1, sizeof pos);

        for(int j = n-1; j > i; --j){
            rep(k, 2){
                int fat = k ? 1 : -1, p = (input[j] - input[i]) / fat;

//              if(mk[input[j]][k] == it) continue;
//              mk[input[j]][k] = it;
                int val = geta(j, input[j], p, 1-k);
//              printf("(%d,%d) . %d\n", i, j, val);
                pd[i][j][k] = 1 + val;
                if(!k) best = max(best, pd[i][j][k]);
            }
            pos[input[j]] = j;
        }
//      ++it;
    }
}

int main(){
    scanf("%d", &n);
    rep(i, n) scanf("%d", input + i);

    faz();
    printf("%d\n", best);
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define fr(i,a,b) for(int i = a; i <  b; ++i)
#define rep(i,n) fr(i,0,n)
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define N 4010

int pd[N][N][2];

int input[N];
int n;

vector<int> pos[1000010];

int geta(int j, int val, int p, int k){
    int nt = val + (k ? 1 : -1) * p;
    if(nt > 1000000 || nt < 0) return 1;

    vector<int>::iterator qual = lower_bound(pos[nt].begin(), pos[nt].end(), j+1);
    int ind = -1;
    if(qual != pos[nt].end()){
        ind = *qual;
    }

//  if(i == 2 && j == 3 && k == 0) printf(">>> %d %d|%d| > %d\n", nt, qual, n, input[j+1]);
//  if(i == 1 && j == 2 && p == 10) printf(">>> %d %d\n", nt, qual);
    if(ind != -1)
        return pd[j][ind][k];
    else
        return 1;
}

int best;
int mk[1000100][2];
void faz(){
    best = 1;
    int it = 1;

    for(int i = n - 2; i >= 0; --i){
        for(int j = i + 1; j < n; ++j){
            rep(k, 2){
                int fat = k ? 1 : -1, p = (input[j] - input[i]) / fat;

//              if(!p){
//                  pd[i][j][k] = -10000000;
//                  continue;
//              }

                if(mk[input[j]][k] == it) continue;
                mk[input[j]][k] = it;
                int val = geta(j, input[j], p, 1-k);
                pd[i][j][k] = 1 + val;
                if(!k) best = max(best, pd[i][j][k]);
            }
        }
        ++it;
    }
}

int main(){
    scanf("%d", &n);
    rep(i, n) scanf("%d", input + i), pos[input[i]].pb(i);

    faz();
    printf("%d\n", best);
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define fr(i,a,b) for(int i = a; i <  b; ++i)
#define rep(i,n) fr(i,0,n)
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define N 4010

int pd[N][N][2];

int input[N];
int n;

int pos[1000010];

int geta(int j, int val, int p, int k){
    int nt = val + (k ? 1 : -1) * p;
    if(nt > 1000000 || nt < 0) return 1;
    int ind = pos[nt];

//  printf("> %d %d %d\n", j, nt, ind);
    if(ind > j)
        return pd[j][ind][k];
    else
        return 1;
}

int best;
//int mk[1000100][2];
void faz(){
    best = 1;
    int it = 1;
    memset(pos, -1, sizeof pos);

    for(int i = n - 2; i >= 0; --i){

        for(int j = n-1; j > i; --j){
            rep(k, 2){
                int fat = k ? 1 : -1, p = (input[j] - input[i]) / fat;

//              if(mk[input[j]][k] == it) continue;
//              mk[input[j]][k] = it;
                int val = geta(j, input[j], p, 1-k);
//              printf("(%d,%d) . %d\n", i, j, val);
                pd[i][j][k] = 1 + val;
                if(!k) best = max(best, pd[i][j][k]);
            }
            pos[input[j]] = j;
        }
//      ++it;
    }
}

int main(){
    scanf("%d", &n);
    rep(i, n) scanf("%d", input + i);

    faz();
    printf("%d\n", best);
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define fr(i,a,b) for(int i = a; i <  b; ++i)
#define rep(i,n) fr(i,0,n)
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define N 4010

int pd[N][N][2];

int input[N];
int n;

int pos[1000010];

int geta(int j, int val, int p, int k){
    int nt = val + (k ? 1 : -1) * p;
    if(nt > 1000000 || nt < 0) return 1;
    int ind = pos[nt];

    if(ind != -1)
        return pd[j][ind][k];
    else
        return 1;
}

int best;
//int mk[1000100][2];
void faz(){
    best = 1;
    int it = 1;

    for(int i = n - 2; i >= 0; --i){
        memset(pos, -1, sizeof pos);

        for(int j = n-1; j > i; --j){
            rep(k, 2){
                int fat = k ? 1 : -1, p = (input[j] - input[i]) / fat;

//              if(mk[input[j]][k] == it) continue;
//              mk[input[j]][k] = it;
                int val = geta(j, input[j], p, 1-k);
                pd[i][j][k] = 1 + val;
                if(!k) best = max(best, pd[i][j][k]);
                pos[input[j]] = j;
            }
        }
//      ++it;
    }
}

int main(){
    scanf("%d", &n);
    rep(i, n) scanf("%d", input + i);

    faz();
    printf("%d\n", best);
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define fr(i,a,b) for(int i = a; i <  b; ++i)
#define rep(i,n) fr(i,0,n)
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define N 4010

int pd[N][N][2];

int input[N];
int n;

int low(int i, int val){
    int ini = i , fim = n - 1;
    int resp = -1;

    while(ini <= fim){
        int mid = (ini+fim)/2;
        if(input[mid] >= val){
            fim = mid - 1;
            if(input[mid] == val) resp = mid;
        }else
            ini = mid + 1;
    }
    return resp;
}

vector<int> pos[1000010];

int geta(int i, int j, int val, int p, int k){
    int nt = val + (k ? 1 : -1) * p;
    if(nt > 1000000 || nt < 0) return 1;

    vector<int>::iterator qual = lower_bound(pos[nt].begin(), pos[nt].end(), j+1);
    int ind = -1;
    if(qual != pos[nt].end()){
        ind = *qual;
    }

//  if(i == 2 && j == 3 && k == 0) printf(">>> %d %d|%d| > %d\n", nt, qual, n, input[j+1]);
//  if(i == 1 && j == 2 && p == 10) printf(">>> %d %d\n", nt, qual);
    if(ind != -1)
        return pd[j][ind][k];
    else
        return 1;
}

int best;

void faz(){
    best = 1;

    for(int i = n - 2; i >= 0; --i){
        for(int j = i + 1; j < n; ++j){
            rep(k, 2){
                int fat = k ? 1 : -1, p = (input[j] - input[i]) / fat;

                if(!p){
                    pd[i][j][k] = -10000000;
                    continue;
                }
                int val = geta(i, j, input[j], p, 1-k);
                pd[i][j][k] = 1 + val;
//              printf("(%d,%d) %d %d %d\n", i, j, k, p, 1+val);
                if(!k) best = max(best, pd[i][j][k]);
            }
        }
    }
}

int main(){
    scanf("%d", &n);
    rep(i, n) scanf("%d", input + i), pos[input[i]].pb(i);

    faz();
    printf("%d\n", best);
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define fr(i,a,b) for(int i = a; i <  b; ++i)
#define rep(i,n) fr(i,0,n)
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define N 4010

int pd[N][N][2];

int input[N];
int n;

vector<int> pos[1000010];

int geta(int i, int j, int val, int p, int k){
    int nt = val + (k ? 1 : -1) * p;
    if(nt > 1000000 || nt < 0) return 1;

    vector<int>::iterator qual = lower_bound(pos[nt].begin(), pos[nt].end(), j+1);
    int ind = -1;
    if(qual != pos[nt].end()){
        ind = *qual;
    }

//  if(i == 2 && j == 3 && k == 0) printf(">>> %d %d|%d| > %d\n", nt, qual, n, input[j+1]);
//  if(i == 1 && j == 2 && p == 10) printf(">>> %d %d\n", nt, qual);
    if(ind != -1)
        return pd[j][ind][k];
    else
        return 1;
}

int best;
int mk[1000100][2];
void faz(){
    best = 1;
    int it = 1;

    for(int i = n - 2; i >= 0; --i){
        for(int j = i + 1; j < n; ++j){
            rep(k, 2){
                int fat = k ? 1 : -1, p = (input[j] - input[i]) / fat;

                if(!p){
                    pd[i][j][k] = -10000000;
                    continue;
                }

                if(mk[input[j]][k] == it) continue;
                mk[input[j]][k] = it;
                int val = geta(i, j, input[j], p, 1-k);
                pd[i][j][k] = 1 + val;
                if(!k) best = max(best, pd[i][j][k]);
            }
        }
        ++it;
    }
}

int main(){
    scanf("%d", &n);
    rep(i, n) scanf("%d", input + i), pos[input[i]].pb(i);

    faz();
    printf("%d\n", best);
}


#define dbg if(1)
#define dout dbg cout
#define $(v) #v << ": " << (v) << " "
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<functional>
#include<algorithm>
#include<utility>
#include<list>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
#define FWD(i,b,e) for(int i=(b);i<(e);++i)
#define FOR(i,n) FWD(i,0,n)
#define BWD(i,b,e) for(int i=(int)(e)-1;i>=(b);--i)
typedef long long LL;
typedef pair<int,int> PII;
typedef double K;
const int OO = (1<<30)-1;
const int MAXN = (1<<12)+1;
const int MAXDIFF = (1<<20)+1;
const int NIL = -1;

int b[MAXN];
int seq[2*MAXDIFF];
int endseq[2*MAXDIFF];

int main() {
  int n;
  int res = 0;
  scanf("%d", &n);
  FOR(i,2*MAXDIFF) endseq[i] = NIL;
  FOR(i,n) {
    scanf("%d", &b[i]);
    FOR(j,i) {
      int diff = b[i] - b[j];
      if (endseq[MAXDIFF-diff] == NIL or endseq[MAXDIFF-diff] == j) {
        if (seq[MAXDIFF+diff] < seq[MAXDIFF-diff] + 1) {
          seq[MAXDIFF+diff] = seq[MAXDIFF-diff] + 1;
          endseq[MAXDIFF+diff] = i;
        }
      }
    }
  }
  FOR(i,2*MAXDIFF)
      res = max(res, seq[i]);
  printf("%d\n", res+1);
  return 0;
}


#define dbg if(1)
#define dout dbg cout
#define $(v) #v << ": " << (v) << " "
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<functional>
#include<algorithm>
#include<utility>
#include<list>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
#define FWD(i,b,e) for(int i=(b);i<(e);++i)
#define FOR(i,n) FWD(i,0,n)
#define BWD(i,b,e) for(int i=(int)(e)-1;i>=(b);--i)
typedef long long LL;
typedef pair<int,int> PII;
typedef double K;
const int OO = (1<<30)-1;
const int MAXN = (1<<12)+1;
const int MAXB = (1<<20)+1;

int b[MAXN];
int cnt[MAXN];
int lasti[MAXN];

int main() {
  int n;
  int res = 0;
  scanf("%d", &n);
  FOR(i,n) {
    scanf("%d", &b[i]);
  }
  FOR(i,n) {
    int icnt = 1;
    FWD(j,i+1,n) {
      if (b[j] == b[i]) icnt++;
      else if (lasti[b[j]] != icnt) {
        cnt[b[j]]++;
        lasti[b[j]] = icnt;
      }
    }
    FWD(j,i+1,n) {
      res = max(res, 2*cnt[b[j]] + (lasti[b[j]] != icnt));
      cnt[b[j]] = 0;
      lasti[b[j]] = 0;
    }
  }
  printf("%d\n", res);
  return 0;
}


#define dbg if(1)
#define dout dbg cout
#define $(v) #v << ": " << (v) << " "
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<functional>
#include<algorithm>
#include<utility>
#include<list>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
#define FWD(i,b,e) for(int i=(b);i<(e);++i)
#define FOR(i,n) FWD(i,0,n)
#define BWD(i,b,e) for(int i=(int)(e)-1;i>=(b);--i)
#define FOREACH(it,c) for(__typeof((c).end()) it = (c).begin();it!=(c).end();++it)
#define ALL(c) (c).begin(),(c).end()
typedef long long LL;
typedef pair<int,int> PII;
typedef double K;
const int OO = (1<<30)-1;
const int MAXN = (1<<12)+1;

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

int main() {
  int n;
  int res = 0;
  scanf("%d", &n);
  FOR(i,n) {
    scanf("%d", &b[i]);
    FOR(j,i) {
      int diff = b[i] - b[j];
      seq[i][diff] = max(seq[i][diff], seq[j][-diff]+1);
    }
  }
  FOR(i,n)
    FOREACH(it, seq[i]) {
      res = max(res, it->second);
    }
  printf("%d\n", res+1);
  return 0;
}


#define dbg if(1)
#define dout dbg cout
#define $(v) #v << ": " << (v) << " "
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<functional>
#include<algorithm>
#include<utility>
#include<list>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
#define FWD(i,b,e) for(int i=(b);i<(e);++i)
#define FOR(i,n) FWD(i,0,n)
#define BWD(i,b,e) for(int i=(int)(e)-1;i>=(b);--i)
typedef long long LL;
typedef pair<int,int> PII;
typedef double K;
const int OO = (1<<30)-1;
const int MAXN = (1<<12)+1;
const int MAXB = (1<<20)+1;

int b[MAXN];
int cnt[MAXB];
int lasti[MAXB];

int main() {
  int n;
  int res = 1;
  scanf("%d", &n);
  FOR(i,n) {
    scanf("%d", &b[i]);
  }
  FOR(i,n) {
    int icnt = 1;
    FWD(j,i+1,n) {
      if (b[j] == b[i]) icnt++;
      else if (lasti[b[j]] != icnt) {
        cnt[b[j]]++;
        lasti[b[j]] = icnt;
      }
    }
    FWD(j,i+1,n) {
      res = max(res, 2*cnt[b[j]] + (lasti[b[j]] != icnt));
      cnt[b[j]] = 0;
      lasti[b[j]] = 0;
    }
    res = max(res, icnt);
  }
  printf("%d\n", res);
  return 0;
}


#define dbg if(1)
#define dout dbg cout
#define $(v) #v << ": " << (v) << " "
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<functional>
#include<algorithm>
#include<utility>
#include<list>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
#define FWD(i,b,e) for(int i=(b);i<(e);++i)
#define FOR(i,n) FWD(i,0,n)
#define BWD(i,b,e) for(int i=(int)(e)-1;i>=(b);--i)
typedef long long LL;
typedef pair<int,int> PII;
typedef double K;
const int OO = (1<<30)-1;
const int MAXN = (1<<12)+1;
const int MAXB = (1<<20)+1;

int b[MAXN];
int cnt[MAXN];
int lasti[MAXN];

int main() {
  int n;
  int res = 1;
  scanf("%d", &n);
  FOR(i,n) {
    scanf("%d", &b[i]);
  }
  FOR(i,n) {
    int icnt = 1;
    FWD(j,i+1,n) {
      if (b[j] == b[i]) icnt++;
      else if (lasti[b[j]] != icnt) {
        cnt[b[j]]++;
        lasti[b[j]] = icnt;
      }
    }
    FWD(j,i+1,n) {
      res = max(res, 2*cnt[b[j]] + (lasti[b[j]] != icnt));
      cnt[b[j]] = 0;
      lasti[b[j]] = 0;
    }
  }
  printf("%d\n", res);
  return 0;
}


#define dbg if(1)
#define dout dbg cout
#define $(v) #v << ": " << (v) << " "
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<functional>
#include<algorithm>
#include<utility>
#include<list>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
#define FWD(i,b,e) for(int i=(b);i<(e);++i)
#define FOR(i,n) FWD(i,0,n)
#define BWD(i,b,e) for(int i=(int)(e)-1;i>=(b);--i)
typedef long long LL;
typedef pair<int,int> PII;
typedef double K;
const int OO = (1<<30)-1;
const int MAXN = (1<<12)+1;
const int MAXB = (1<<20)+1;

int b[MAXN];
int cnt[MAXB];
int lasti[MAXB];

int main() {
  int n;
  int res = 1;
  scanf("%d", &n);
  FOR(i,n) {
    scanf("%d", &b[i]);
  }
  FOR(i,n) {
    int icnt = 1;
    FWD(j,i+1,n) {
      if (b[j] == b[i]) icnt++;
      else if (lasti[b[j]] != icnt) {
        cnt[b[j]]++;
        lasti[b[j]] = icnt;
      }
    }
    FWD(j,i+1,n) {
      res = max(res, 2*cnt[b[j]] + (lasti[b[j]] != icnt));
      cnt[b[j]] = 0;
      lasti[b[j]] = 0;
    }
  }
  printf("%d\n", res);
  return 0;
}


#include <cstdio> //PROG: CF156A
#include <cstring>
#include <algorithm>
#include <map>
using std::map;
using std::max;
map<int,int> nid;
map<int,int> DP[5000]; int N,ary[5000],_nid[5000];
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF156A_in.txt","r",stdin);
	freopen("CF156A_out.h","w",stdout);
	#endif
	int i,j,q,ans=1,x,n,m,nidn;
	map<int,int>::iterator itr;
	scanf("%d",&N);
	for (i=0;i<N;i++){
		scanf("%d",ary+i);
		nid[ary[i]]=1;
	}
	for (i=0,itr=nid.begin();itr!=nid.end();itr++,i++){
		itr->second=i;
		_nid[itr->second]=itr->first;
	}
	nidn=i;
	for (n=1;n<N;n++){
		bool samer=false;
		for (m=n-1;m>=0;m--){
			i=nid[ary[n]];
			j=nid[ary[m]];
			if (i==j){
				if (samer)continue;
				samer=true;
			}
			q=ary[n]-ary[m];
			if (DP[j][-q]==0)DP[j][-q]=x=1;
			else x=DP[j][-q];
			DP[i][q]=max(DP[i][q],x+1);
			//printf("n=%d <%d->%d> DP(%d,%d)=%d\n",n,j,i,i,q,DP[i][q]);
		}
	}
	for (i=0;i<nidn;i++)
		for (itr=DP[i].begin(); itr!=DP[i].end(); itr++){
			//printf("i=%d(ref%d) %d -> %d\n",i,_nid[i],itr->first,itr->second);
			ans=max(ans,itr->second);
		}
	printf("%d\n",ans);
}


#include <cstdio> //PROG: CF156A
#include <cstring>
#include <algorithm>
#include <map>
using std::map;
using std::max;
map<int,int> DP[5000]; int N,ary[5000];
int dif(int a,int b){return a>b?(a-b):(b-a);}
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF156A_in.txt","r",stdin);
	freopen("CF156A_out.h","w",stdout);
	#endif
	int i,j,q,ans=1,x;
	map<int,int>::iterator itr;
	scanf("%d",&N);
	for (i=0;i<N;i++)
		scanf("%d",ary+i);
	for (i=1;i<N;i++)
		for (j=0;j<i;j++){
			q=dif(ary[i],ary[j]);
			if (j==0)x=1;
			else x=DP[j][q];
			DP[i][q]=max(DP[i][q],x+1);
		}
	for (itr=DP[N-1].begin(); itr!=DP[N-1].end(); itr++)
		ans=max(ans,itr->second);
	printf("%d\n",ans);
}


#include <cstdio> //PROG: CF156A
#include <cstring>
#include <algorithm>
#include <map>
using std::map;
using std::max;
map<int,int> nid;
map<int,int> DP[5000]; int N,ary[5000];
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF156A_in.txt","r",stdin);
	freopen("CF156A_out.h","w",stdout);
	#endif
	int i,j,q,ans=1,x,n,m;
	map<int,int>::iterator itr;
	scanf("%d",&N);
	for (i=0;i<N;i++){
		scanf("%d",ary+i);
		nid[ary[i]]=1;
	}
	for (i=0,itr=nid.begin();itr!=nid.end();itr++,i++)
		itr->second=i;
	for (n=1;n<N;n++){
		for (m=0;m<n;m++){
			i=nid[ary[n]];
			j=nid[ary[m]];
			q=ary[n]-ary[m];
			if (DP[j][-q]==0)x=1;
			else x=DP[j][-q];
			DP[i][q]=max(DP[i][q],x+1);
		}
	}
	for (i=0;i<N;i++)
		for (itr=DP[i].begin(); itr!=DP[i].end(); itr++)
			ans=max(ans,itr->second);
	printf("%d\n",ans);
}


#include <cstdio> //PROG: CF156A
#include <cstring>
#include <algorithm>
#include <map>
using std::map;
using std::max;
map<int,int> DP[5000]; int N,ary[5000];
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF156A_in.txt","r",stdin);
	freopen("CF156A_out.h","w",stdout);
	#endif
	int i,j,q,ans=1,x;
	map<int,int>::iterator itr;
	scanf("%d",&N);
	for (i=0;i<N;i++)
		scanf("%d",ary+i);
	for (i=1;i<N;i++)
		for (j=0;j<i;j++){
			q=ary[i]-ary[j];
			if (DP[j][-q]==0)x=1;
			else x=DP[j][-q];
			DP[i][q]=max(DP[i][q],x+1);
			//printf("DP(%d,%d)=%d (x=%d)\n",i,q,DP[i][q],x);
		}
	for (i=0;i<N;i++)
		for (itr=DP[i].begin(); itr!=DP[i].end(); itr++)
			ans=max(ans,itr->second);
	printf("%d\n",ans);
}


#include <cstdio> //PROG: CF156A
#include <cstring>
#include <algorithm>
#include <map>
using std::map;
using std::max;
map<int,int> DP[5000]; int N,ary[5000];
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF156A_in.txt","r",stdin);
	freopen("CF156A_out.h","w",stdout);
	#endif
	int i,j,q,ans=1,x;
	map<int,int>::iterator itr;
	scanf("%d",&N);
	for (i=0;i<N;i++)
		scanf("%d",ary+i);
	for (i=1;i<N;i++)
		for (j=0;j<i;j++){
			q=ary[i]-ary[j];
			if (j==0)x=1;
			else x=DP[j][-q];

			if (DP[i][q]==0)DP[i][q]=1;
			DP[i][q]=max(DP[i][q],x+1);
		}
	for (i=0;i<N;i++)
		for (itr=DP[i].begin(); itr!=DP[i].end(); itr++)
			ans=max(ans,itr->second);
	printf("%d\n",ans);
}


#include <cstdio> //PROG: CF156A
#include <cstring>
#include <algorithm>
#include <map>
using std::map;
using std::max;
map<int,int> DP[5000]; int N,ary[5000];
int dif(int a,int b){return (a>b)?(a-b):(b-a);}
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF156A_in.txt","r",stdin);
	freopen("CF156A_out.h","w",stdout);
	#endif
	int i,j,q,ans=1,x;
	map<int,int>::iterator itr;
	scanf("%d",&N);
	for (i=0;i<N;i++)
		scanf("%d",ary+i);
	for (i=1;i<N;i++)
		for (j=0;j<i;j++){
			q=dif(ary[i],ary[j]);
			if (j==0)x=1;
			else x=DP[j][q];

			if (DP[i][q]==0)DP[i][q]=1;
			DP[i][q]=max(DP[i][q],x+1);
		}
	for (itr=DP[N-1].begin(); itr!=DP[N-1].end(); itr++)
		ans=max(ans,itr->second);
	printf("%d\n",ans);
}


#include <cstdio> //PROG: CF156A
#include <cstring>
#include <algorithm>
#include <map>
using std::map;
using std::max;
map<int,int> DP[5000]; int N,ary[5000];
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF156A_in.txt","r",stdin);
	freopen("CF156A_out.h","w",stdout);
	#endif
	int i,j,q,ans=1,x;
	map<int,int>::iterator itr;
	scanf("%d",&N);
	for (i=0;i<N;i++)
		scanf("%d",ary+i);
	for (i=1;i<N;i++)
		for (j=0;j<i;j++){
			q=ary[i]-ary[j];
			if (j==0)x=1;
			else x=DP[j][-q];

			if (DP[i][q]==0)DP[i][q]=1;
			DP[i][q]=max(DP[i][q],x+1);
		}
	for (itr=DP[N-1].begin(); itr!=DP[N-1].end(); itr++)
		ans=max(ans,itr->second);
	printf("%d\n",ans);
}


#include <cstdio> //PROG: CF156A
#include <cstring>
#include <algorithm>
#include <map>
using std::max;
typedef std::map<int,int> MAP;
MAP map;
int ary[4000],N,DP[4000][4001];
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF156A_in.txt","r",stdin);
	freopen("CF156A_out.h","w",stdout);
	#endif
	int i,j,idn,n,m,ans=1;
	scanf("%d",&N);
	for (j=i=0;i<N;i++){
		scanf("%d",ary+i);
		if (!map[ary[i]])map[ary[i]]=++j;
		ary[i]=map[ary[i]];
	}
	idn=j;
	for (i=1;i<N;i++)
		for (j=0;j<i;j++){
			n=ary[i];
			m=ary[j];
			if (DP[j][n]==0)DP[j][n]=1;
			DP[i][m]=max(DP[i][m],DP[j][n]+1);
			ans=max(ans,DP[i][m]);
		}
	printf("%d\n",ans);

}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <utility>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#define fr(a,b,c) for (int a=b;a<=c;a++)
#define frr(a,b,c) for (int a=b;a>=c;a--)
#define rep(a,b) for (int a=0;a<b;a++)
#define repp(a,b) for (int a=b-1;a>=0;a--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define reset(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define oo 1000111222
#define maxN 1
using namespace std;

int main()
{
	int n, a[4444], ans = 0, cnt[4444], last[4444];
	vector <int> X;
	cin >> n;
	rep(i,n) cin >> a[i], X.pb(a[i]);
	sort(all(X));
	X.resize(unique(all(X)) - X.begin());
	rep(i,n) a[i] = lower_bound(all(X), a[i]) - X.begin();

	rep(i,n)
	{
		int lim = i, x = 1;
		reset(cnt, 0);
		reset(last, -1);
		fr(j,i+1,n-1)
			if (a[j] == a[i]) x++, lim = j;
			else
				if (last[a[j]] < lim) cnt[a[j]]++, last[a[j]] = j;

		rep(j,sz(X))
			if (last[j] > lim) ans = max(ans, cnt[j] * 2);
			else ans = max(ans, cnt[j] * 2 + 1);

		ans = max(ans, x);
	}

	cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <utility>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#define fr(a,b,c) for (int a=b;a<=c;a++)
#define frr(a,b,c) for (int a=b;a>=c;a--)
#define rep(a,b) for (int a=0;a<b;a++)
#define repp(a,b) for (int a=b-1;a>=0;a--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define reset(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define oo 1000111222
#define maxN 1
using namespace std;

int main()
{
	int n, a[4444], ans = 0, cnt[4444], last[4444];
	vector <int> X;
	cin >> n;
	rep(i,n) cin >> a[i], X.pb(a[i]);
	sort(all(X));
	X.resize(unique(all(X)) - X.begin());
	rep(i,n) a[i] = lower_bound(all(X), a[i]) - X.begin();

	rep(i,n)
	{
		int lim = i, x = 1;
		reset(cnt, 0);
		reset(last, -1);
		fr(j,i+1,n-1)
			if (a[j] == a[i]) x++, lim = j;
			else
				if (last[a[j]] < lim) cnt[a[j]]++, last[a[j]] = j;

		int mx = 0;
		rep(j,sz(X)) mx = max(mx, cnt[j]);
		ans = max(ans, x + mx);
	}

	cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <utility>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#define fr(a,b,c) for (int a=b;a<=c;a++)
#define frr(a,b,c) for (int a=b;a>=c;a--)
#define rep(a,b) for (int a=0;a<b;a++)
#define repp(a,b) for (int a=b-1;a>=0;a--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define reset(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define oo 1000111222
#define maxN 1
using namespace std;

int main()
{
	int n, a[4444], ans = 0, cnt[4444], last[4444];
	vector <int> X;
	cin >> n;
	rep(i,n) cin >> a[i], X.pb(a[i]);
	sort(all(X));
	X.resize(unique(all(X)) - X.begin());
	rep(i,n) a[i] = lower_bound(all(X), a[i]) - X.begin();

	rep(i,n)
	{
		int lim = i, x = 1;
		reset(cnt, 0);
		reset(last, -1);
		fr(j,i+1,n-1)
			if (a[j] == a[i]) x++, lim = j;
			else
				if (last[a[j]] < lim) cnt[a[j]]++, last[a[j]] = j;

		rep(j,sz(X))
			if (last[j] > lim) ans = max(ans, cnt[j] * 2);
			else ans = max(ans, cnt[j] * 2 + 1);
	}

	cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <utility>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#define fr(a,b,c) for (int a=b;a<=c;a++)
#define frr(a,b,c) for (int a=b;a>=c;a--)
#define rep(a,b) for (int a=0;a<b;a++)
#define repp(a,b) for (int a=b-1;a>=0;a--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define reset(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define oo 1000111222
#define maxN 1
using namespace std;

int main()
{
	int n, a[4444], ans = 0, cnt[4444], last[4444];
	vector <int> X;
	cin >> n;
	rep(i,n) cin >> a[i], X.pb(a[i]);
	sort(all(X));
	X.resize(unique(all(X)) - X.begin());
	rep(i,n) a[i] = lower_bound(all(X), a[i]) - X.begin();

	rep(i,n)
	{
		int lim = i, x = 1;
		reset(cnt, 0);
		reset(last, -1);
		fr(j,i+1,n-1)
			if (a[j] == a[i]) x++, lim = j;
			else
				if (last[a[j]] < lim) cnt[a[j]]++, last[a[j]] = j;

		int mx = 0;
		rep(j,sz(X)) mx = max(mx, cnt[j]);
		if (x == mx) ans = max(ans, x * 2);
		else ans = max(ans, mx * 2 + 1);
	}

	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define db(x) {if(debug){cout << #x << " = " << (x) << endl;}}
#define dba(a, x, y) {if(debug){cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}}
#define mp make_pair
#define pb push_back
#define endl '\n'
#define ll long long
#define ld long double
#define pi 3.1415926535897932384626433832795028
const int INF = INT_MAX;
const bool debug = true;
// const int MOD = ;
const ll INFL = LLONG_MAX;
const int output_precision = 15;

stringstream ss;
int N, B[5100], nxt[5100];
map<int,int> prev;
int main()
{
  ios_base::sync_with_stdio(0); cout.precision(output_precision); cout << fixed;
  ss.precision(output_precision); ss << fixed;
  cin >> N;
  rep1(i,N)cin>>B[i];

  for (int i = N; i >= 1; i--)
  {
    if (prev[B[i]]>0)
      nxt[i]=prev[B[i]];
    else
      nxt[i]=-1;
    prev[B[i]]=i;
  }
//  dba(nxt, 1, N);

  int ans = 1;
  rep1(i, N) rep1(j, N) if (i <= j)
  {
    int a = i;
    int b = j;
    int cnt = 2;
//    db(a); db(b); exit(0);
    while (true)
    {
      while (a != -1 && a <= b)
      {
        a = nxt[a];
//        db(a);
      }
      if (a != -1) cnt++;
      else break;

      while (b != -1 && b <= a)
      {
        b = nxt[b];
      }
      if (b != -1) cnt++;
      else break;
//    db(a); db(b); exit(0);
    }
    ans = max(ans,cnt);
  }
  cout << ans << endl;

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define db(x) {if(debug){cout << #x << " = " << (x) << endl;}}
#define dba(a, x, y) {if(debug){cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}}
#define mp make_pair
#define pb push_back
#define endl '\n'
#define ll long long
#define ld long double
#define pi 3.1415926535897932384626433832795028
const int INF = INT_MAX;
const bool debug = true;
// const int MOD = ;
const ll INFL = LLONG_MAX;
const int output_precision = 15;

stringstream ss;
int N, B[5100], nxt[5100];
map<int,int> prev;
set< pair<int,int> > checked;
int main()
{
  ios_base::sync_with_stdio(0); cout.precision(output_precision); cout << fixed;
  ss.precision(output_precision); ss << fixed;
  cin >> N;
  rep1(i,N)cin>>B[i];

  for (int i = N; i >= 1; i--)
  {
    if (prev[B[i]]>0)
      nxt[i]=prev[B[i]];
    else
      nxt[i]=-1;
    prev[B[i]]=i;
  }
//  dba(nxt, 1, N);

  int ans = 1;
  rep1(i, N)
  {
    set<int> s;
    s.clear();
    rep1(j, N)
    {
      if (i <= j)
      {
        int a = i;
        int b = j;
        if (s.find(B[j]) != s.end()) continue;
        s.insert(B[j]);

        int cnt = i==j?1:2;
        //    db(a); db(b); exit(0);
        while (true)
        {
          while (a != -1 && a <= b)
          {
            a = nxt[a];
            //        db(a);
          }
          if (a != -1) cnt++;
          else break;

          while (b != -1 && b <= a)
          {
            b = nxt[b];
          }
          if (b != -1) cnt++;
          else break;
          //    db(a); db(b); exit(0);
        }
        if (ans < cnt)
        {
          ans=cnt;
          //      db(i); db(j); db(ans);
        }
      }
    }
  }
  cout << ans << endl;

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define db(x) {if(debug){cout << #x << " = " << (x) << endl;}}
#define dba(a, x, y) {if(debug){cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}}
#define mp make_pair
#define pb push_back
#define endl '\n'
#define ll long long
#define ld long double
#define pi 3.1415926535897932384626433832795028
const int INF = INT_MAX;
const bool debug = true;
// const int MOD = ;
const ll INFL = LLONG_MAX;
const int output_precision = 15;

stringstream ss;
int N, B[5000];
map<int, vector<int> > indices;
map<int,int> cnt;
vector<int> v;
int main()
{
  ios_base::sync_with_stdio(0); cout.precision(output_precision); cout << fixed;
  ss.precision(output_precision); ss << fixed;
  cin >> N;
  rep1(i,N) cin >> B[i];
  int mx = 1;

  rep1(i, N) v.pb(B[i]);
  rep1(i, N) cnt[B[i]]++, mx=max(mx,cnt[B[i]]);
  rep1(i, N) indices[B[i]].pb(i);

  sort(v.begin(), v.end());
  v.erase(unique(v.begin(),v.end()),v.end());
  rep(j, v.size()) rep(i, j)
  {
    // i < j
    int cur = indices[v[i]][0];
    int cur_cnt = 1;
    vector<int> v2;
    v2.pb(v[i]);
    v2.pb(v[j]);
    vector<int> v3;
    v3.pb(1);
    v3.pb(0);
    while (cur<=N)
    {
      int ind = v3[cur_cnt&1];
      while (ind < indices[v2[cur_cnt&1]].size()
          && indices[v2[cur_cnt&1]][ind] < cur)
      {
        ind++;
      }
      if (ind < indices[v2[cur_cnt&1]].size())
      {
        v3[cur_cnt&1]=ind+1;
        cur = ind;
        cur_cnt++;
      }
      else
      {
        break;
      }
    }
    mx = max(mx,cur_cnt);

  }
  cout << mx << endl;
  exit(0);

  rep1(a, N) rep1(b, N) if (a < b)
  {
    if (B[a]==B[b])
    {
    }

  }
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define For(i, a, b) for (int (i) = (a); (i) <= (b); (i) ++)
#define db(x) {if(debug){cout << #x << " = " << (x) << endl;}}
#define dba(a, x, y) {if(debug){cout << #a << " :";For(i, (x), (y))cout<<" "<<(a)[(i)];cout<<endl;}}
#define mp make_pair
#define pb push_back
#define endl '\n'
#define ll long long
#define ld long double
#define pi 3.1415926535897932384626433832795028
const int INF = INT_MAX;
const bool debug = true;
// const int MOD = ;
const ll INFL = LLONG_MAX;
const int output_precision = 15;

stringstream ss;
int N, B[5100], nxt[5100];
map<int,int> prev;
int main()
{
  ios_base::sync_with_stdio(0); cout.precision(output_precision); cout << fixed;
  ss.precision(output_precision); ss << fixed;
  cin >> N;
  rep1(i,N)cin>>B[i];

  for (int i = N; i >= 1; i--)
  {
    if (prev[B[i]]>0)
      nxt[i]=prev[B[i]];
    else
      nxt[i]=-1;
    prev[B[i]]=i;
  }
//  dba(nxt, 1, N);

  int ans = 1;
  rep1(i, N) rep1(j, N) if (i <= j)
  {
    int a = i;
    int b = j;
    int cnt = i==j?1:2;
//    db(a); db(b); exit(0);
    while (true)
    {
      while (a != -1 && a <= b)
      {
        a = nxt[a];
//        db(a);
      }
      if (a != -1) cnt++;
      else break;

      while (b != -1 && b <= a)
      {
        b = nxt[b];
      }
      if (b != -1) cnt++;
      else break;
//    db(a); db(b); exit(0);
    }
    if (ans < cnt)
    {
      ans=cnt;
//      db(i); db(j); db(ans);
    }
  }
  cout << ans << endl;

}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int n;
int b[5000]={0};
int dp[5000][5000]={0};
int ans=0;
int h[1000010]={0};
int hh[1000010]={0};
int t=0;
int Max=0;

int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
        if(h[b[i]]==0)h[b[i]]=++t;
        hh[b[i]]++;
        Max=max(Max,hh[b[i]]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j<=n;j++)
        {
            int tmp1=h[b[i]],tmp2=h[b[j]];
            if(tmp1==tmp2)continue;
            dp[tmp1][tmp2]=dp[tmp2][tmp1]+1;
            ans=max(ans,dp[tmp1][tmp2]);
        }
    }
    ans=max(ans+1,Max);
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

int n;
int save[40040];
int cnt[40040];
int vis[40040];

int main()
{
    std::ios::sync_with_stdio(false);
    cin >> n;
    int i;
    for (i = 0; i < n; i++)
    {
        cin >> save[i];
    }
    int j;
    memset (cnt,0,sizeof(cnt));
    memset (vis,0,sizeof(vis));
    for (i = 0; i < n; i++)
    {
        if (vis[i] == 1)
        {
            cnt[i] = cnt[i-2]-1;
            continue;
        }
        vis[i] = 1;
        int sum = 1;
        for (j = i+2; j < n; j += 2)
        {
            if (save[j] == save[i])
            {
                sum++;
            }
            else
            {
                break;
            }
        }
        cnt[i] = sum;
    }
    int res = 1;
    for (i = 1; i < n; i++)
    {
        int tmp;
        int x = cnt[i-1];
        int y = cnt[i];
        if (x <= y)
        {
            tmp = x + x;
        }
        else if (x > y)
        {
            tmp = y + y + 1;
        }
        if (tmp > res)
        {
            res = tmp;
        }
    }
    cout << res;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#define MAXN 1000
#define offset 10000
#define eps 1e-11
#define PI acos(-1.0)//3.14159265358979323846
#define exp 2.718281828
#define mod 1000000007
using namespace std;
typedef long long LL;

int a[1000005];
int dp[4001][4001];
int main(){

	for(int i = 1;i<=4000;i++){
		for(int j = 1;j<=4000;j++){
		dp[i][j] = 1;
	}
	}
	int n;
	scanf("%d",&n);
	int maxx = 1;
	for(int i = 0;i<n;i++){
		scanf("%d",&a[i]);
		int b[4001] = {0};
		for(int j = 0;j<i;j++){
		//	dp[a[i]][a[j]] =dp[a[j]][a[i]] + 1;
	//	cout<<i<<' '<<j<<' '<<b[a[j]]<<endl;
		if(b[a[j]] != 1){
	//		cout<<a[i]<<' '<<a[j]<<endl;
		//	dp[a[i]][a[j]] =dp[a[j]][a[i]] + 1;
			dp[a[i]][a[j]] =max(dp[a[i]][a[j]],dp[a[j]][a[i]] + 1);
			b[a[j]] = 1;
		}
			maxx = max(maxx,dp[a[i]][a[j]]);
		}
	}

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

	return 0;
	}


#include<cstdio>
#include<cmath>
#include<stdlib.h>
#include<map>
#include<time.h>
#include<vector>
#include<queue>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int dp[4005][4005];
int num[4005];
map <int,int>r;
int pos=1;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&num[i]);
        if(r[num[i]]==0)
            r[num[i]]=pos++;
    }
    for(int i=0;i<=n;i++)
    {
        dp[i][0]=1;
    }
    for(int i=1;i<=n;i++)
    {
        int term=0;
        for(int j=1;j<=pos;j++)
        {
            for(int k=0;k<i;k++)
            {
                if(r[num[k]]==j)
                {
                    dp[i][j]=max(dp[i][j],max(dp[k][r[num[i]]],dp[i][0])+1);
                }
            }
            dp[i][j]=max(dp[i][j],dp[i-1][j]);
        }
    }
//    for(int i=0;i<=n;i++)
//    {
//        for(int j=1;j<=pos;j++)
//            printf("%d ",dp[i][j]);
//        printf("\n");
//    }
    int ans=0;
    for(int i=1;i<=pos;i++)
    {
        ans=max(ans,dp[n][i]);
    }
    printf("%d\n",ans);
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>

using namespace std;
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define mk make_pair
#define pb push_back
#define sz size()
#define AA first
#define BB second
#define eps 1e-10
#define zero(x) fabs(x) < eps
#define deq(a , b) fabs(a - b) < eps
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1

const int MAXN = 1000 + 5;
const int MAXE = 50000 + 50;
const int MAXQ = 1000000 + 50;

const int inf = 0x3f3f3f3f;
const int INF = ~0U >> 1;
const long long LLinf = 0x3FFFFFFFFFFFFFFFLL;
const long long LLINF = (1LL << 63) - 1;
const int IMIN = 0x80000000;

const int mod = 10007;
const long long MOD = 1000000000 + 7;
const double PI = acos(-1.0);

typedef long long LL;
typedef pair<int , int> pii;
typedef vector<int> vec;

int num[MAXN];
vec vnum[1000000 + 10];
vec hasnum;
int main()
{
//    freopen("wormhole.in" , "r" , stdin);
//    freopen("wormhole.out" , "w" , stdout);
    int n;
    while(~scanf("%d" , &n)){
        for(int i = 1 ; i <= 1e6 ; i++){
            vnum[i].clear();
        }
        hasnum.clear();
        for(int i = 0 ; i < n ; i++){
            scanf("%d" , &num[i]);
            hasnum.pb(num[i]);
            vnum[num[i]].pb(i);
        }
        for(int i = 1 ; i <= 1e6 ; i++){
            sort(vnum[i].begin() , vnum[i].end());
        }
        int ans = 0;
        for(int i = 0 ; i < hasnum.sz ; i++){
            for(int j = i + 1 ; j < hasnum.sz ; j++){
                int a = hasnum[i] , b = hasnum[j];
//                for(int i = 0 ; i < vnum[a].sz ; i++){
//                    cout << vnum[a][i] << ' ' ;
//                }cout << endl;
//                for(int i = 0 ; i < vnum[b].sz ; i++){
//                    cout << vnum[b][i] << ' ' ;
//                }cout << endl;
                int ret1 = 0 , ret2 = 0;
                int pos = -1;
                for(int p = 0 ; p < vnum[a].sz ; p++){
                    int st = upper_bound(vnum[b].begin() , vnum[b].end() , vnum[a][p]) - vnum[b].begin();
//                    cout << "p num[a][p] st num[b][st] pos________________" << p << ' ' << vnum[a][p] << ' ' << st << ' ' <<vnum[b][st] << ' ' << pos << endl;
                    if(vnum[b][st] == pos)continue;
                    if(st == vnum[b].sz){
                        ret1 ++;
                        break;
                    }
                    ret1 += 2;
                    pos = vnum[b][st];
                }
                pos = -1;
                for(int p = 0 ; p < vnum[b].sz ; p++){
                    int st = upper_bound(vnum[a].begin() , vnum[a].end() , vnum[b][p]) - vnum[a].begin();
                    if(vnum[a][st] == pos)continue;
                    if(st == vnum[a].sz){
                        ret2++;
                        break;
                    }
                    ret2 += 2;
                    pos = vnum[a][st];
                }
//                cout << "abret               " << a << ' ' << b << ' ' << ret1 << ' ' << ret2 << endl;
                ans = max(ans , max(ret1 , ret2));
            }
        }
        printf("%d\n" , ans);
    }
    return 0;
}
/*

*/


//#include <bits/stdc++.h>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
#define MP make_pair
#define PB push_back
#define SZ size()
#define AA first
#define BB second
#define BG begin()
#define ED end()
#define SQ(x) ((x)*(x))
#define cmin(x,y) x=min(x,y)
#define cmax(x,y) x=max(x,y)

#define Maxn 41111
vector<int> a, b, g[Maxn];
map<int,int> mp;
int mmp[Maxn];
int n;

int calc(int ii, int jj) {
    int i, j, k, u, v, cnt = 1;
    i = 1; j = 0;
    u = g[ii][0];
    v = -1;
    while(i < g[ii].SZ || j < g[jj].SZ) {
        //printf("i %d j %d cnt %d u %d v %d\n", i, j, cnt, u, v);
        if(cnt & 1) {
            while(j < g[jj].SZ) {
                if(g[jj][j] > u) {
                    break;
                }
                j++;
            }
            if(j >= g[jj].SZ) {
                return cnt;
            }
            cnt++;
            v = g[jj][j];
            j++;
        }
        else {
            while(i < g[ii].SZ) {
                if(g[ii][i] > v) {
                    break;
                }
                i++;
            }
            if(i >= g[ii].SZ) {
                return cnt;
            }
            u = g[ii][i];
            cnt++;
            i++;
        }
    }
    return cnt;
}

int main() {
    int i, j, k, u, v, w;

    while(cin >> n) {
        a.clear();
        b.clear();
        for(i = 0; i < n; i++) {
            cin >> w;
            a.PB(w);
            b.PB(w);
        }
        sort(b.BG, b.ED);
        mp.clear();
        b.erase(unique(b.BG, b.ED), b.ED);
        for(i = 0; i < b.SZ; i++) {
            mp[b[i]] = i + 1;
            mmp[i + 1] = b[i];
        }
        for(i = 0; i < n; i++) a[i] = mp[a[i]];
        for(i = 0; i <= n; i++) g[i].clear();
        for(i = 0; i < n; i++) {
            g[a[i]].PB(i);
        }
        for(i = 0; i <= b.SZ; i++) {
            //for(j = 0; j < g[i].SZ; j++) printf(" %d", g[i][j]);
            //printf("\n");
        }
        int ans = 1;
        for(i = 1; i <= b.SZ; i++) cmax(ans, (int)g[i].SZ);
        for(i = 1; i <= b.SZ; i++) {
            for(j = 1; j <= b.SZ; j++) {
                if(j == i) continue;
                //printf("calc i %d j %d %d \n", i, j, calc(i, j));
                cmax(ans, calc(i, j));
            }
        }
        cout << ans << endl;
    }

    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<vector>
using namespace std;
void main(){
	int n,ans=0,max=-1000000;
	cin>>n;
	vector < pair<int , int> > a(n);
	int *b=new int[n];
	for(int i=0;i<n;i++){
		cin>>a[i].first;
		b[i]=a[i].first;
		a[i].second=i;
	}
	std::sort(a.begin() , a.end());
	max=a[0].first;

	for(int i=0;i<a[0].second;i++){
		if(b[i]>max)
			max=b[i];
	}
	if(a[0].first < max){
	for(int i=0;i<n;i++){
		if(a[i].second != i){
			ans = max - a[i].first;
			break;
		}
	}
	}
	cout<<ans;

}


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<vector>
using namespace std;
void main(){
	int n,ans=0,max=-1000000,k=0;
	cin>>n;
	vector < pair<int , int> > a(n);
	int *b=new int[n];
	int *m=new int[n];
	m[0]=0;
	for(int i=0;i<n;i++){
		cin>>a[i].first;
		b[i]=a[i].first;
		a[i].second=i;
	}
	std::sort(a.begin() , a.end());

	for(int j=0;j<n;j++){
	max=a[j].first;

	for(int i=0;i<a[j].second;i++){
		if(b[i]>max)
			max=b[i];
	}
	if(a[j].first < max){
	//for(int i=0;i<n;i++){
		//if(a[i].second != i){
			m[k] = max - a[0].first;
			k++;
			//break;
		//}
	//}
	}
	}
	if(k>0){
		std::sort(m,m+k);
		cout<<m[k-1];
	}
	else cout<<0;

}


#include<stdio.h>
#define maxn 100005
int main(){
    int n;
    int num[maxn];
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&num[i]);
    int ans=0;
    for(int i=2;i<=n;i++)
    {
        if(num[i]<num[i-1])
            ans+=(num[i-1]-num[i]);
    }
    printf("%d\n",ans);
    return 0;
}


#include<stdio.h>
#define maxn 100005
typedef long long ll;
ll num[maxn];
int main(){
    ll n;
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++)
        scanf("%lld",&num[i]);
    ll ans=0;
    for(ll i=2;i<=n;i++)
    {
        if(num[i]<num[i-1])
            ans+=(num[i-1]-num[i]);
    }
    printf("%lld\n",ans);
    return 0;
}


#include<stdio.h>
#define maxn 10005
int main(){
    int n;
    int num[maxn];
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&num[i]);
    int ans=0;
    for(int i=2;i<=n;i++)
    {
        if(num[i]<num[i-1])
            ans+=(num[i-1]-num[i]);
    }
    printf("%d\n",ans);
    return 0;
}


#include<stdio.h>
#define maxn 10005
int main(){
    int n;
    int num[maxn];
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&num[i]);
    int ans=0;
    for(int i=2;i<=n;i++)
    {
        if(num[i]<num[i-1])
            ans+=(num[i]-num[i-1]);
    }
    printf("%d\n",ans);
    return 0;
}


#include<stdio.h>
//#include<iostream>
#define maxn 10005
//using namespace std;
typedef long  long ll;
ll num[maxn];
int main(){
    ll n;
    scanf("%lld",&n);
    for( int i = 1 ; i <= n ; i ++){
        scanf("%lld",&num[i]);
    }
    ll ans;
    ans=0;
    for(int i=2;i<=n;i++ ){
        while(i<=n&&num[i]>=num[i-1]){
            i++;
        }
        if(i>n)break;
        ll tmp;
        tmp=(num[i-1]-num[i]);
        ans+=tmp;
        for(int j=i;j<=n;j++){
            if(num[j]==num[i-1]||num[j]>num[i-1])
                break;
            num[j]+=tmp;
        }
        //printf("tmp:%d\n",tmp);
        //for(int k=1;k<=n;k++)printf("%d ",num[k]);
        //printf("\n\n");
    }
    printf("%lld\n",ans);
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <math.h>
#include <queue>
using namespace std;
int main()
{
    //freopen("1.txt","r",stdin);
    //freopen("2.txt","w",stdout);
    int n;
    cin>>n;
    vector <int > v(1,0);
    for(int i=0;i<n;i++)
    {
        int a;
        scanf("%d",&a);
        if(v.back()!=a)
            v.push_back(a);
    }
    long long ans=0;
    for(int i=2;i<v.size();i++)
        ans+=max(v[i-1]-v[i],0);
    printf("%I64d\n",ans);
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <math.h>
#include <queue>
using namespace std;
int main()
{
    //freopen("1.txt","r",stdin);
    //freopen("2.txt","w",stdout);
    int n;
    cin>>n;
    vector <int > v(1,0);
    for(int i=0;i<n;i++)
    {
        int a;
        scanf("%d",&a);
        if(v.back()!=a)
            v.push_back(a);
    }
    int ans=0;
    for(int i=2;i<v.size();i++)
        ans+=max(v[i-1]-v[i],0);
    cout<<ans<<endl;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <math.h>
#include <queue>
using namespace std;
int main()
{
    //freopen("1.txt","r",stdin);
    //freopen("2.txt","w",stdout);
    int n;
    cin>>n;
    vector <int > v(1,0);
    for(int i=0;i<n;i++)
    {
        int a;
        scanf("%d",&a);
        if(v.back()!=a)
            v.push_back(a);
    }
    long long ans=0;
    for(int i=2;i<v.size();i++)
        ans+=max(v[i-1]-v[i],0);
    printf("I64d\n",ans);
}


#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<utility>
#include<set>
#include<deque>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<list>
#include<stack>
#include<queue>
#include<sstream>
#include<cstring>
#include<functional>
using namespace std;

#pragma comment(linker, "/STACK:64000000")

typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef long long LLL;
typedef unsigned long long HL;
const double EPS = 1e-8;
const int INF = 1e9;
const LLL LINF = 1e18;

int n;
LLL a, k;
LLL ans;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif

	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>k;
		ans+=max(0LL, a-k);
		a = k;
	}
	cout<<ans;

    return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<utility>
#include<set>
#include<deque>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<list>
#include<stack>
#include<queue>
#include<sstream>
#include<cstring>
#include<functional>
using namespace std;

#pragma comment(linker, "/STACK:64000000")

typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef long long LLL;
typedef unsigned long long HL;
const double EPS = 1e-8;
const int INF = 1e9;
const LLL LINF = 1e18;

const int MAXN = 100010;
int n;
LLL a[MAXN];
LLL ans;

LLL calc(vector<LLL>& v, int l, int r)
{
	while(l<r && v[l]==0LL)
		l++;
	if(l>=r)
		return 0LL;
	LLL m = 1e15;
	int mi;
	for(int i=l;i<r;i++)
		if(v[i]<m)
		{
			m = v[i];
			mi = i;
		}
	for(int i=l;i<r;i++)
		v[i]-=m;
	return calc(v, l, mi)+calc(v, mi+1, r)+m;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif

	cin>>n;
	a[n] = 1e10;
	for(int i=0;i<n;i++)
		cin>>a[i];
	for(int i=1;i<n;i++)
		if(a[i]<a[i-1])
		{
			vector<LLL> dif;
			for(int j=i;j<=n;j++)
				if(a[j]<a[i-1])
					dif.push_back(a[i-1]-a[j]);
				else
				{
					a[j-1] = a[i-1];
					i = j-1;
					break;
				}
			//sort(dif.begin(), dif.end());
			ans+=calc(dif, 0, dif.size());
		}
	cout<<ans;

    return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<utility>
#include<set>
#include<deque>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<list>
#include<stack>
#include<queue>
#include<sstream>
#include<cstring>
#include<functional>
using namespace std;

#pragma comment(linker, "/STACK:64000000")

typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef long long LLL;
typedef unsigned long long HL;
const double EPS = 1e-8;
const int INF = 1e9;
const LLL LINF = 1e18;

const int MAXN = 100010;
int n;
LLL a[MAXN];
LLL ans;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif

	cin>>n;
	a[n] = 1e10;
	for(int i=0;i<n;i++)
		cin>>a[i];
	for(int i=1;i<n;i++)
		if(a[i]<a[i-1])
		{
			vector<LLL> dif;
			for(int j=i;j<=n;j++)
				if(a[j]<a[i-1])
					dif.push_back(a[i-1]-a[j]);
				else
				{
					a[j-1] = a[i-1];
					i = j-1;
					break;
				}
			sort(dif.begin(), dif.end());
			LLL cur = 0LL;
			for(int j=0;j<dif.size();j++)
			{
				ans+=dif[j]-cur;
				cur = dif[j];
			}
		}
	cout<<ans;

    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
long long count;
void inc(vector<long long>*arr, unsigned int *start) {
    long long tempM, max = 0;
    unsigned int end = (*start);
    for(unsigned int i = (*start)+1; i < arr->size(); i++) {
        if((*arr)[i] < (*arr)[i-1]) {
            end++;
        } else
            break;
    }
    if((*start) == end) {
        count += (*arr)[(*start)-1] - (*arr)[(*start)];
        (*arr)[(*start)] += (*arr)[(*start)-1] - (*arr)[(*start)];
    }
    else {
        for(unsigned int i = (*start); i <= end; i++) {
            tempM = (*arr)[(i-1)] - (*arr)[(i)];
            if(tempM > max) {
                max = tempM;
            }
            (*arr)[i]+=tempM;
        }
        count+=max;
    }
    (*start) = end;
}
int main() {
    long long size, temp;
    count = 0;
    cin >> size;
    vector<long long> arr;
    while(size != 0) {
        cin >> temp; arr.push_back(temp);
        size--;
    }
    for(unsigned int i = 1; i < arr.size(); i++) {
        if(arr[i] < arr[i-1]) {
            inc(&arr, &i);
        }
    }
    cout << count << endl;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int count;
void inc(vector<int>*arr, int start) {
    int end = start;
    for(unsigned int i = start+1; i < arr->size(); i++) {
        if((*arr)[i] < (*arr)[i-1]) {
            end++;
        }
    }
    for(int i = start; i <= end; i++) {
        (*arr)[i] = (*arr)[i]+1;
    }
    count++;
}
int main() {
    int size, temp;
    count = 0;
    cin >> size;
    vector<int> arr;
    while(size != 0) {
        cin >> temp; arr.push_back(temp);
        size--;
    }
    for(unsigned int i = 1; i < arr.size(); i++) {
        while(arr[i] < arr[i-1]) {
            inc(&arr, i);
        }
    }
    cout << count << endl;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
long long count;
void inc(vector<long long>*arr, unsigned int *start) {
    long long tempM, max = 0;
    unsigned int end = (*start);
    for(unsigned int i = (*start)+1; i < arr->size(); i++) {
        if((*arr)[i] < (*arr)[i-1]) {
            end++;
        } else
            break;
    }
    if((*start) == end) {
        count += (*arr)[(*start)-1] - (*arr)[(*start)];
        (*arr)[(*start)] += (*arr)[(*start)-1] - (*arr)[(*start)];
    }
    else {
        for(unsigned int i = (*start); i <= end; i++) {
            tempM = (*arr)[(i-1)] - (*arr)[(i)];
            if(tempM > max) {
                max = tempM;
            }
            (*arr)[i]+=tempM;
        }
        count+=max;
    }
    //(*start) = end;
}
int main() {
    long long size, temp;
    count = 0;
    cin >> size;
    vector<long long> arr;
    while(size != 0) {
        cin >> temp; arr.push_back(temp);
        size--;
    }
    for(unsigned int i = 1; i < arr.size(); i++) {
        if(arr[i] < arr[i-1]) {
            inc(&arr, &i);
        }
    }
    cout << count << endl;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
long long count;
void inc(vector<int>*arr, unsigned int *start) {
    int end = (*start), tempM, max = 0;
    vector<int>temp = *arr;
    for(unsigned int i = (*start)+1; i < arr->size(); i++) {
        if((*arr)[i] < (*arr)[i-1]) {
            end++;
        } else
            break;
    }
    if((*start) == end) {
        count += (*arr)[(*start)-1] - (*arr)[(*start)];
        (*arr)[(*start)] += (*arr)[(*start)-1] - (*arr)[(*start)];
    }
    else {
        for(int i = (*start); i <= end; i++) {
            tempM = (*arr)[(i-1)] - (*arr)[(i)];
            if(tempM > max) {
                max = tempM;
            }
            (*arr)[i]+=tempM;
        }
        count+=max;
    }
    (*start) = end;
}
int main() {
    int size, temp;
    count = 0;
    cin >> size;
    vector<int> arr;
    while(size != 0) {
        cin >> temp; arr.push_back(temp);
        size--;
    }
    for(unsigned int i = 1; i < arr.size(); i++) {
        if(arr[i] < arr[i-1]) {
            inc(&arr, &i);
        }
    }
    cout << count << endl;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
long long count;
void inc(vector<int>*arr, unsigned int *_start) {
    int start = *_start, end = start, tempM, max = 0;
    vector<int>temp = *arr;
    for(unsigned int i = start+1; i < arr->size(); i++) {
        if((*arr)[i] < (*arr)[i-1]) {
            end++;
        } else
            break;
    }
    if(start == end) {
        count += (*arr)[start-1] - (*arr)[start];
        (*arr)[start] += (*arr)[start-1] - (*arr)[start];
    }
    else {
        for(int i = start; i <= end; i++) {
            tempM = (*arr)[start-1] - (*arr)[start];
            if(tempM > max) {
                max = tempM;
            }
            //(*arr)[i]++;
        }
        count++;
    }
}
int main() {
    int size, temp;
    count = 0;
    cin >> size;
    vector<int> arr;
    while(size != 0) {
        cin >> temp; arr.push_back(temp);
        size--;
    }
    for(unsigned int i = 1; i < arr.size(); i++) {
        if(arr[i] < arr[i-1]) {
            inc(&arr, &i);
        }
    }
    cout << count << endl;
    return 0;
}


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

int main() {
    long long size, count;
    int temp;
    vector<int> arr;
    while(cin >> size) {
        count = 0;
        for(int i = 0; i < size; i++) {
            cin >> temp; arr.push_back(temp);
        }
        for(unsigned int i = 1; i < arr.size(); i++) {
            if(arr[i] < arr[i-1]) {
                count += arr[i-1] - arr[i];
            }
        }
        cout << count << endl;
        arr.clear();
    }
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int count;
void inc(vector<int>*arr, int start) {
    int end = start;
    vector<int>temp = *arr;
    for(unsigned int i = start+1; i < arr->size(); i++) {
        if((*arr)[i] < (*arr)[i-1]) {
            end++;
        } else
            break;
    }
    if(start == end) {
        count += (*arr)[start-1] - (*arr)[start];
        (*arr)[start] += (*arr)[start-1] - (*arr)[start];
    }
    else {
        for(int i = start; i <= end; i++) {
            (*arr)[i]++;
        }
        count++;
    }
}
int main() {
    int size, temp;
    count = 0;
    cin >> size;
    vector<int> arr;
    while(size != 0) {
        cin >> temp; arr.push_back(temp);
        size--;
    }
    for(unsigned int i = 1; i < arr.size(); i++) {
        while(arr[i] < arr[i-1]) {
            inc(&arr, i);
        }
    }
    cout << count << endl;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
long long count;
void inc(vector<int>*arr, int start) {
    int end = start;
    vector<int>temp = *arr;
    for(unsigned int i = start+1; i < arr->size(); i++) {
        if((*arr)[i] < (*arr)[i-1]) {
            end++;
        } else
            break;
    }
    if(start == end) {
        count += (*arr)[start-1] - (*arr)[start];
        (*arr)[start] += (*arr)[start-1] - (*arr)[start];
    }
    else {
        for(int i = start; i <= end; i++) {
            (*arr)[i]++;
        }
        count++;
    }
}
int main() {
    int size, temp;
    count = 0;
    cin >> size;
    vector<int> arr;
    while(size != 0) {
        cin >> temp; arr.push_back(temp);
        size--;
    }
    for(unsigned int i = 1; i < arr.size(); i++) {
        while(arr[i] < arr[i-1]) {
            inc(&arr, i);
        }
    }
    cout << count << endl;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
long long count;
void inc(vector<long long>*arr, unsigned int *start) {
    long long tempM, max = 0;
    unsigned int end = (*start);
    for(unsigned int i = (*start)+1; i < arr->size(); i++) {
        if((*arr)[i] <= (*arr)[i-1]) {
            end++;
        } else
            break;
    }
    /*if((*start) == end) {
        count += (*arr)[(*start)-1] - (*arr)[(*start)];
        (*arr)[(*start)] += (*arr)[(*start)-1] - (*arr)[(*start)];
    }
    else {*/
        for(unsigned int i = (*start); i <= end; i++) {
            tempM = (*arr)[(i-1)] - (*arr)[(i)];
            if(tempM > max) {
                max = tempM;
            }
            (*arr)[i]+=tempM;
        }
        count+=max;
    //}
    //(*start) = end;
}
int main() {
    long long size, temp;
    count = 0;
    cin >> size;
    vector<long long> arr;
    while(size != 0) {
        cin >> temp; arr.push_back(temp);
        size--;
    }
    for(unsigned int i = 1; i < arr.size(); i++) {
        if(arr[i] < arr[i-1]) {
            inc(&arr, &i);
        }
    }
    cout << count << endl;
    return 0;
}


#include <iostream>
#include <fstream>
#include <conio.h>
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <bitset>
#include <math.h>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <map>
#include <set>
using namespace std;

bool cmp(pair<int,int> a, pair<int,int> b){
    return a.first < b.first;
}

int main(){
    //freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
    int n;
    cin >> n;
    vector<int>vec(n),sorted;
    for(int i=0; i<n; ++i) scanf("%d", &vec[i]);
    sorted = vec;
    sort(sorted.begin(),sorted.end());
    if(n==1 || sorted==vec){
        cout << 0;
    }

    else{
        int plus = 0;
        for(int i=1; i<n; ++i){
            vec[i]+=plus;
            if(vec[i]<vec[i-1]){
                plus+=vec[i-1]-vec[i];
                vec[i]=vec[i-1];
            }
        }
        cout << plus;
    }

    return 0;
}


#include <iostream>
#include <fstream>
#include <conio.h>
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <bitset>
#include <math.h>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <map>
#include <set>
using namespace std;

bool cmp(pair<int,int> a, pair<int,int> b){
    return a.first < b.first;
}

int main(){
    //freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
    int n;
    cin >> n;
    vector<int>vec(n),sorted;
    for(int i=0; i<n; ++i) scanf("%d", &vec[i]);
    sorted = vec;
    sort(sorted.begin(),sorted.end());
    if(n==1 || sorted==vec){
        cout << 0;
        return 0;
    }
    else{
        int mn=199999999;
        for(int i=1; i<vec.size(); ++i) if(vec[i]<mn) mn=vec[i];
        cout << vec[0]-mn;
        return 0;
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <conio.h>
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <bitset>
#include <math.h>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <map>
#include <set>
using namespace std;

bool cmp(pair<long long,long long> a, pair<long long,long long> b){
    return a.first < b.first;
}

int main(){
    //freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
    long long n;
    cin >> n;
    vector<long long>vec(n),sorted;
    for(long long i=0; i<n; ++i) scanf("%d", &vec[i]);
    sorted = vec;
    sort(sorted.begin(),sorted.end());
    if(n==1 || sorted==vec){
        cout << 0;
    }

    else{
        long long plus = 0;
        for(long long i=1; i<n; ++i){
            vec[i]+=plus;
            if(vec[i]<vec[i-1]){
                plus+=vec[i-1]-vec[i];
                vec[i]=vec[i-1];
            }
        }
        cout << plus;
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
using namespace std;
int a[100001]
int main()
{
    int n;
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    int count1=0;
    for(int i=1;i<n;i++)
    {
        if (a[i]<a[i-1])
        {
            count1+=a[i-1]-a[i];
        }
    }
    cout<<count1;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
using namespace std;
int a[100001];
int main()
{
    int n;
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    int count1=0;
    for(int i=1;i<n;i++)
    {
        if (a[i]<a[i-1])
        {
            count1+=a[i-1]-a[i];
        }
    }
    cout<<count1;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
using namespace std;
int a[100001];
int main()
{
        int n;
        cin>>n;
        for (int i=0;i<n;i++)
        {
                cin>>a[i];
        }
        long long count1=0;
        for(int i=1;i<n;i++)
        {
                if (a[i]<a[i-1])
                {
                        count1+=a[i-1]-a[i];
                }
        }
        cout<<count1;
        return 0;
}


//	#include "stdafx.h"
	#include <iostream>
	#include <conio.h>
	#pragma comment (linker, "/STACK:16777216")
	#include <vector>
	#include <math.h>
	#include <algorithm>
	#include <string>
	#include <queue>
	#include <stack>
	#include <set>
	using namespace std;

int main()
{
	int n;
	cin>>n;

	long long up=0, a, max=-1, cou=0, upper=0;
	cin>>max;

	for (int i=1; i<n; i++)
	{
		cin>>a;
		a+=up;
		if (a>max)
		{
			max=a;
		}
		else
		{
			upper=max-a;
			if (upper>0)
			{
				cou+=upper;
				up+=upper;
			}
		}
	}

	cout<<up;

//	_getch();
	return 0;
}


	//#include "stdafx.h"
	#include <iostream>
	#include <conio.h>
	#pragma comment (linker, "/STACK:16777216")
	#include <vector>
	#include <math.h>
	#include <algorithm>
	#include <string>
	#include <queue>
	#include <stack>
	#include <set>
	using namespace std;

int main()
{
	int n;
	cin>>n;

	long long up=0, a, max=-1, cou=0, upper=0;
	cin>>max;

	for (int i=1; i<n; i++)
	{
		cin>>a;
		if (a>max)
		{
			max=a;
		}
		else
		{
			upper=max-up-a;
			if (upper>0)
			{
				cou+=upper;
				up+=upper;
			}
		}
	}

	cout<<up;

	//_getch();
	return 0;
}


//	#include "stdafx.h"
	#include <iostream>
	#include <conio.h>
	#pragma comment (linker, "/STACK:16777216")
	#include <vector>
	#include <math.h>
	#include <algorithm>
	#include <string>
	#include <queue>
	#include <stack>
	#include <set>
	using namespace std;

int main()
{
	int n;
	cin>>n;

	long long up=0, a, max=-1, cou=0, upper=0;
	cin>>max;

	for (int i=1; i<n; i++)
	{
		cin>>a;
		if (a>max)
		{
			max=a;
		}
		else
		{
			upper=max-up-a;
			if (upper>0)
			{
				cou+=upper;
				up+=upper;
			}
		}
	}

	cout<<up;

//	_getch();
	return 0;
}


	//#include "stdafx.h"
	#include <iostream>
	#include <conio.h>
	#pragma comment (linker, "/STACK:16777216")
	#include <vector>
	#include <math.h>
	#include <algorithm>
	#include <string>
	#include <queue>
	#include <stack>
	#include <set>
	using namespace std;

int main()
{
	int n;
	cin>>n;

	long long up=0, a, max=-1, cou=0, upper=0;
	cin>>max;

	for (int i=1; i<n; i++)
	{
		cin>>a;
		if (a>max)
		{
			max=a;
		}
		else
		{
			upper=max-up-a;
			if (upper>0)
			{
				cou+=upper;
				up+=upper;
			}
		}
	}

	cout<<up;

//	_getch();
	return 0;
}


#include <iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
  //  freopen("test.in", "rt", stdin ) ;
    long long int n,m,c = 0 , x  , lst ;
    scanf("%I64d",&n) ;
    scanf("%I64d",&lst) ;
    n--;
    while(n--) {
        scanf("%I64d",&m) ;
        x = m - lst + c  ;
        if ( x >= 0 )
            lst = m + c ;
        else {
            c+=(-1*x );
        }
    }
    cout << c << endl;
    return 0;
}


#include <iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
    //freopen("test.in", "rt", stdin ) ;
    long long int n,m,c = 0 , x  , lst ;
    scanf("%I64d",&n) ;
    scanf("%I64d",&lst) ;
    n--;
    while(n--) {
        scanf("%I64d",&m) ;
        x = m - lst + c  ;

        if ( x >= 0 )
            lst = m;
        else {
            c+=(-1*x );
        }
    }
    cout << c << endl;
    return 0;
}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	int n, a[100010];
	cin >> n;
	cin >> a[0];
	int tot = 0;
	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		if (a[i]<a[i - 1])
		{
			tot += a[i - 1] - a[i];
		}
	}
	cout << tot;
	return 0;
}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	long long n, a[1000100];
	cin >> n;
	cin >> a[0];
	int tot = 0;
	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		if (a[i]<a[i - 1])
		{
			tot += a[i - 1] - a[i];
		}
	}
	cout << tot;
	return 0;
}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	long long n, a[100010];
	cin >> n;
	cin >> a[0];
	long long tot = 0;
	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		if (a[i]<a[i - 1])
		{
			tot += a[i - 1] - a[i];
		}
	}
	cout << tot;
	system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	long long n, a[100010];
	cin >> n;
	cin >> a[0];
	long long tot = 0;
	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		if (a[i]<a[i - 1])
		{
			tot += a[i - 1] - a[i];
		}
	}
	cout << tot;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <string>
#include <numeric>
#include <functional>
#include <queue>

#define all(x) (x).begin(), (x).end()
#define LL long long
#define ULL unsigned long long
#define mems(a, v) memset(a, v, sizeof(a))
#define forn(n) for (int i = 0; i < n; ++i)
#define EPS 1e-9
#define INF 1e9
#define MOD 1000000000 + 7

using namespace std;

int n;
vector<LL> a;

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
    cin >> n;

    a.resize(n, 0);
    forn(n) cin >> a[i];

    LL counter = 0;
    for (int i = 1; i < n; ++i) {
        if (a[i] + counter < a[i - 1]) {
            counter += a[i - 1] - a[i] - counter;
        }
        a[i] += counter;
    }

    cout << counter << endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <string>
#include <numeric>
#include <functional>
#include <queue>

#define all(x) (x).begin(), (x).end()
#define LL long long
#define ULL unsigned long long
#define mems(a, v) memset(a, v, sizeof(a))
#define forn(n) for (int i = 0; i < n; ++i)
#define EPS 1e-9
#define INF 1e9
#define MOD 1000000000 + 7

using namespace std;

int n;
vector<LL> a;

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
    cin >> n;

    a.resize(n, 0);
    forn(n) cin >> a[i];

    LL counter = 0;
    for (int i = 1; i < n; ++i) {
        if (a[i] + counter < a[i - 1]) {
            counter += a[i - 1] - a[i] - counter;
            a[i] += counter;
        }
    }

    cout << counter << endl;

    return 0;
}


#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{

    long long n = 0, a[100001], steps = 0;

    cin >> n;

    for(long long i = 0; i < n; ++i)
    {
        cin >> a[i];
    }

    for(long long i = 0; i < n - 1; ++i)
    {
        if(a[i] > a[i+1])
        {
            steps += a[i] - a[i+1];
        }

    }

    cout << steps << endl;

    return 0;
}


#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{

    int n = 0, a[100001], steps = 0, preSteps = 0;

    cin >> n;

    for(int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }

    for(int i = 0; i < n - 1; ++i)
    {
        while(a[i] + preSteps > a[i+1] + steps)
        {
            ++steps;
        }

        preSteps = steps;
    }

    cout << steps << endl;

    return 0;
}


#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{

    int n = 0, a[100001], steps = 0;

    cin >> n;

    for(int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }

    for(int i = 0; i < n - 1; ++i)
    {
        while(a[i] > a[i+1])
        {
            ++steps;

            for(int j = i+1; j < n; ++j)
            {
                ++a[j];
            }
        }
    }

    cout << steps << endl;

    return 0;
}


#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{

    int n = 0, a[100001], steps = 0, preSteps = 0;

    cin >> n;

    for(int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }

    for(int i = 0; i < n - 1; ++i)
    {
        if(a[i] + preSteps > a[i+1] + steps)
        {
            steps += a[i] + preSteps - (a[i+1] + steps);
        }

        preSteps = steps;
    }

    cout << steps << endl;

    return 0;
}


#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{

    unsigned int n = 0, a[100001], steps = 0;

    cin >> n;

    for(unsigned int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }

    for(unsigned int i = 0; i < n - 1; ++i)
    {
        if(a[i] > a[i+1])
        {
            steps += a[i] - a[i+1];
        }

    }

    cout << steps << endl;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE

#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <assert.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <deque>
#include <math.h>

#define fo(a,b,c) for( a = ( b ); a < ( c ); ++ a )
#define fr(a,b) fo( a, 0, ( b ) )
#define fi(a) fr( i, ( a ) )
#define fj(a) fr( j, ( a ) )
#define fk(a) fr( k, ( a ) )
#define mp make_pair
#define pb push_back
#define all(v) (v).begin( ), (v).end( )
#define _(a,b) memset( a, b, sizeof( a ) )
using namespace std;
int getint() { int a; scanf( "%d", &a ); return a; }
double getdouble() { double a; scanf( "%lf", &a ); return a; }
char sbuf[100005];
string getstring() { scanf( "%s", sbuf ); return sbuf; }
long long getlong() { long long a; scanf( "%lld", &a ); return a; }

template <class T> void out( T a, T b ) { bool first = true; for( T i = a; i != b; ++ i ) { if( !first ) printf( " " ); first = false; cout << * i; } printf( "\n" ); }
template <class T> void outl( T a, T b ) { for( T i = a; i != b; ++ i ) { cout << * i << "\n"; } }
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef map<string,int> msi;

int n, m;
int len[100001] = {0};
int main( )
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
#endif
    vector<long long> v;
    int n;
    cin >> n;
    int i = 0;
    v.resize(n);
    fi(n)
        scanf ("%I64d",&v[i]);
    long long d = 0;
    for (int i = 0; i < n - 1; i++)
    {
        if (v[i] > v[i + 1] + d)
        {
            d += v[i] - (v[i + 1] + d);
            v[i + 1] += d;
        }
    }

    cout << d;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE

#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <assert.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <deque>
#include <math.h>

#define fo(a,b,c) for( a = ( b ); a < ( c ); ++ a )
#define fr(a,b) fo( a, 0, ( b ) )
#define fi(a) fr( i, ( a ) )
#define fj(a) fr( j, ( a ) )
#define fk(a) fr( k, ( a ) )
#define mp make_pair
#define pb push_back
#define all(v) (v).begin( ), (v).end( )
#define _(a,b) memset( a, b, sizeof( a ) )
using namespace std;
int getint() { int a; scanf( "%d", &a ); return a; }
double getdouble() { double a; scanf( "%lf", &a ); return a; }
char sbuf[100005];
string getstring() { scanf( "%s", sbuf ); return sbuf; }
long long getlong() { long long a; scanf( "%lld", &a ); return a; }

template <class T> void out( T a, T b ) { bool first = true; for( T i = a; i != b; ++ i ) { if( !first ) printf( " " ); first = false; cout << * i; } printf( "\n" ); }
template <class T> void outl( T a, T b ) { for( T i = a; i != b; ++ i ) { cout << * i << "\n"; } }
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef map<string,int> msi;

int n, m;
int len[100001] = {0};
int main( )
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
#endif
    vector<long long> v;
    int n;
    cin >> n;
    int i = 0;
    v.resize(n);
    fi(n)
        scanf ("%I64d",&v[i]);
    long long d = 0;
    long long pred = 0;
    for (int i = 0; i < n - 1; i++)
    {
        if (v[i] + d > v[i + 1] + d)
        {
            d += (v[i] + d) - (v[i + 1] + d);
        }
    }

    cout << d;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE

#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <assert.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <deque>
#include <math.h>

#define fo(a,b,c) for( a = ( b ); a < ( c ); ++ a )
#define fr(a,b) fo( a, 0, ( b ) )
#define fi(a) fr( i, ( a ) )
#define fj(a) fr( j, ( a ) )
#define fk(a) fr( k, ( a ) )
#define mp make_pair
#define pb push_back
#define all(v) (v).begin( ), (v).end( )
#define _(a,b) memset( a, b, sizeof( a ) )
using namespace std;
int getint() { int a; scanf( "%d", &a ); return a; }
double getdouble() { double a; scanf( "%lf", &a ); return a; }
char sbuf[100005];
string getstring() { scanf( "%s", sbuf ); return sbuf; }
long long getlong() { long long a; scanf( "%lld", &a ); return a; }

template <class T> void out( T a, T b ) { bool first = true; for( T i = a; i != b; ++ i ) { if( !first ) printf( " " ); first = false; cout << * i; } printf( "\n" ); }
template <class T> void outl( T a, T b ) { for( T i = a; i != b; ++ i ) { cout << * i << "\n"; } }
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef map<string,int> msi;

int n, m;
int len[100001] = {0};
int main( )
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
#endif
    vector<long long> v;
    int n;
    cin >> n;
    int i = 0;
    v.resize(n);
    fi(n)
        scanf ("%lld",&v[i]);
    long long d = 0;
    for (int i = 0; i < n - 1; i++)
    {
        if (v[i] > v[i + 1] + d)
        {
            d += v[i] - (v[i + 1] + d);
            v[i + 1] += d;
        }
    }

    cout << d;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <math.h>
#include <conio.h>
#include <utility>
#include <set>
#include <time.h>
#include <list>
#include <typeinfo>
#include <cstdio>
#include <numeric>
#include <limits>
#include <functional>
using namespace std;
#define X first
#define Y second
#define PI 3.1415926535897932384626433832795
#define PI2 PI * 2
#define sqr(x) ((x)*(x))
#define all(n) (n).begin(), (n).end()
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef pair<double, double> pdd;
typedef vector<pdd> vpdd;
typedef pair<float, float> pff;
const int MOD = (int)1e9 + 7;
typedef double real;

void load(int k = 1) {
	if (k == 1)	{
#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
#endif
	} else {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
}

const int N = (int)1e5 + 10;
const int M = 100 + 2;
LL A[N];

void solve() {
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &A[i]);
	}
	int i = 0;
	LL res = 0, k = 0;
	while (i < n) {
		LL c = 0;
		while (i < n && A[i] < k) {
			c = max(k - A[i], c);
			++i;
		}
		k = A[i];
		++i;
		res += c;
	}
	cout << res;
}

void solve2() {

}

#define TIME_CHECK

int main() {
#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c1 = clock();
#endif
#endif

	load();
	solve();

#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c2 = clock();
	cout << endl << "time: " << (c2 - c1) / (CLOCKS_PER_SEC / 1000);
#endif
#endif
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <math.h>
#include <conio.h>
#include <utility>
#include <set>
#include <time.h>
#include <list>
#include <typeinfo>
#include <cstdio>
#include <numeric>
#include <limits>
#include <functional>
using namespace std;
#define X first
#define Y second
#define PI 3.1415926535897932384626433832795
#define PI2 PI * 2
#define sqr(x) ((x)*(x))
#define all(n) (n).begin(), (n).end()
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef pair<double, double> pdd;
typedef vector<pdd> vpdd;
typedef pair<float, float> pff;
const int MOD = (int)1e9 + 7;
typedef double real;

void load(int k = 1) {
	if (k == 1)	{
#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
#endif
	} else {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
}

const int N = (int)1e5 + 10;
const int M = 100 + 2;
LL A[N];

void solve() {
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> A[i];
	}
	int i = 0;
	LL k = 0;
	LL res = 0;
	while (i < n) {
		LL c = 0;
		while (i < n && A[i] < k) {
			c = max(k - A[i], c);
			++i;
		}
		k = A[i];
		++i;
		res += c;
	}
	cout << res;
}

void solve2() {

}

#define TIME_CHECK

int main() {
#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c1 = clock();
#endif
#endif

	load();
	solve();

#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c2 = clock();
	cout << endl << "time: " << (c2 - c1) / (CLOCKS_PER_SEC / 1000);
#endif
#endif
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <math.h>
#include <conio.h>
#include <utility>
#include <set>
#include <time.h>
#include <list>
#include <typeinfo>
#include <cstdio>
#include <numeric>
#include <limits>
#include <functional>
using namespace std;
#define X first
#define Y second
#define PI 3.1415926535897932384626433832795
#define PI2 PI * 2
#define sqr(x) ((x)*(x))
#define all(n) (n).begin(), (n).end()
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef pair<double, double> pdd;
typedef vector<pdd> vpdd;
typedef pair<float, float> pff;
const int MOD = (int)1e9 + 7;
typedef double real;

void load(int k = 1) {
	if (k == 1)	{
#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
#endif
	} else {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
}

const int N = (int)1e5 + 10;
const int M = 100 + 2;

void solve() {
	int n;
	scanf("%d", &n);
	LL res = 0, cur, prev;
	scanf("%d", &prev);
	for (int i = 1; i < n; ++i) {
		scanf("%d", &cur);
		res += cur < prev ? prev - cur : 0;
		prev = cur;
	}
	printf("%I64d", res);
}

void solve2() {

}

#define TIME_CHECK

int main() {
#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c1 = clock();
#endif
#endif

	load();
	solve();

#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c2 = clock();
	cout << endl << "time: " << (c2 - c1) / (CLOCKS_PER_SEC / 1000);
#endif
#endif
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <math.h>
#include <conio.h>
#include <utility>
#include <set>
#include <time.h>
#include <list>
#include <typeinfo>
#include <cstdio>
#include <numeric>
#include <limits>
#include <functional>
using namespace std;
#define X first
#define Y second
#define PI 3.1415926535897932384626433832795
#define PI2 PI * 2
#define sqr(x) ((x)*(x))
#define all(n) (n).begin(), (n).end()
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef pair<double, double> pdd;
typedef vector<pdd> vpdd;
typedef pair<float, float> pff;
const int MOD = (int)1e9 + 7;
typedef double real;

void load(int k = 1) {
	if (k == 1)	{
#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
#endif
	} else {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
}

const int N = (int)1e5 + 10;
const int M = 100 + 2;
int A[N];

void solve() {
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &A[i]);
	}
	int i = 0, k = 0, res = 0;
	while (i < n) {
		int c = 0;
		while (i < n && A[i] < k) {
			c = max(k - A[i], c);
			++i;
		}
		k = A[i];
		++i;
		res += c;
	}
	cout << res;
}

void solve2() {

}

#define TIME_CHECK

int main() {
#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c1 = clock();
#endif
#endif

	load();
	solve();

#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c2 = clock();
	cout << endl << "time: " << (c2 - c1) / (CLOCKS_PER_SEC / 1000);
#endif
#endif
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <math.h>
#include <conio.h>
#include <utility>
#include <set>
#include <time.h>
#include <list>
#include <typeinfo>
#include <cstdio>
#include <numeric>
#include <limits>
#include <functional>
using namespace std;
#define X first
#define Y second
#define PI 3.1415926535897932384626433832795
#define PI2 PI * 2
#define sqr(x) ((x)*(x))
#define all(n) (n).begin(), (n).end()
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef pair<double, double> pdd;
typedef vector<pdd> vpdd;
typedef pair<float, float> pff;
const int MOD = (int)1e9 + 7;
typedef double real;

void load(int k = 1) {
	if (k == 1)	{
#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
#endif
	} else {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
}

const int N = (int)1e5 + 10;
const int M = 100 + 2;

void solve() {
	int n;
	scanf("%d", &n);
	LL res = 0, cur, prev;
	scanf("%I64d", &prev);
	for (int i = 1; i < n; ++i) {
		scanf("%I64d", &cur);
		res += cur < prev ? prev - cur : 0;
		prev = cur;
	}
	printf("%I64d", res);
}

void solve2() {

}

#define TIME_CHECK

int main() {
#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c1 = clock();
#endif
#endif

	load();
	solve();

#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c2 = clock();
	cout << endl << "time: " << (c2 - c1) / (CLOCKS_PER_SEC / 1000);
#endif
#endif
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <math.h>
#include <conio.h>
#include <utility>
#include <set>
#include <time.h>
#include <list>
#include <typeinfo>
#include <cstdio>
#include <numeric>
#include <limits>
#include <functional>
using namespace std;
#define X first
#define Y second
#define PI 3.1415926535897932384626433832795
#define PI2 PI * 2
#define sqr(x) ((x)*(x))
#define all(n) (n).begin(), (n).end()
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef pair<double, double> pdd;
typedef vector<pdd> vpdd;
typedef pair<float, float> pff;
const int MOD = (int)1e9 + 7;
typedef double real;

void load(int k = 1) {
	if (k == 1)	{
#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
#endif
	} else {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
}

const int N = (int)1e5 + 10;
const int M = 100 + 2;
int A[N];

void solve() {
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &A[i]);
	}
	int i = 0, k = 0;
	LL res = 0;
	while (i < n) {
		int c = 0;
		while (i < n && A[i] < k) {
			c = max(k - A[i], c);
			++i;
		}
		k = A[i];
		++i;
		res += c;
	}
	cout << res;
}

void solve2() {

}

#define TIME_CHECK

int main() {
#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c1 = clock();
#endif
#endif

	load();
	solve();

#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c2 = clock();
	cout << endl << "time: " << (c2 - c1) / (CLOCKS_PER_SEC / 1000);
#endif
#endif
	return 0;
}


#pragma comment(linker, "/STACK:16777216")
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

int main(void)
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif
	int n;
	cin>>n;
	long long answer = 0;
	vector<int> a(n);
	for(int i = 0;i<n;i+=1)
	{
		cin>>a[i];
	}
	for(int i = 1;i<=n-1;i+=1)
	{
		answer +=max(a[i-1] - a[i],0);
	}
	cout<<answer;
	return 0;
}


#pragma comment(linker, "/STACK:16777216")
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

int main(void)
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif
	int n;
	cin>>n;
	long answer = 0;
	vector<int> a(n);
	for(int i = 0;i<n;i+=1)
	{
		cin>>a[i];
	}
	for(int i = 1;i<=n-1;i+=1)
	{
		answer +=max(a[i-1] - a[i],0);
	}
	cout<<answer;
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define f0(i,n) for((int)i=0;(int)i<(int)n;i++)
#define SORT(c) sort((c).begin(),(c).end())
const double PI = acos(-1.0);
const double EPS = 1e-9;
const ll BIG_PRIME7 = 1000000007;
const ll BIG_PRIME9 = 1000000009;

template <typename T>
T gcd(T x, T y) {return y ? gcd(y, x%y): x;}

vector<ll> vi,vv;
vector<string> vs;
pair<ll,ll> pii;
vector<pair <ll, ll> >vp;
set<ll> seti;
map<string,int> mymap;
queue<string> que;

char st[11];
ll  ar[100111];
int arr[200][200];
string s,ss,s1,s2,ss1,ss2,jami;
int q,i,j,k,m,n,h,d,g,t,l,w,r,a,b,c,sum;
ll I,J,L,K,M,N,C,R,SUM,maxi,mini;
bool ind, flag;
double x,y,z,ans;
unsigned long long UI,UJ;

ll f(int l, int r){
	int mini = l;
	for(int i=l;i<=r;i++)
		if(ar[mini]>ar[i])
			mini = i;
	return ar[l] - ar[mini];
}

int main(){
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>ar[i];
	int start = 0;
	int end;
	while(true){
		end = start;
		while( end<n-1 && ar[end]>ar[end+1]){
			end++;
		}
		/*maxi = max(maxi,*/sum+= f(start,end);
	//	sum+=maxi;
		start = end+1;
		if(start>=n) break;
	}
	cout<<sum<<endl;
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define f0(i,n) for((int)i=0;(int)i<(int)n;i++)
#define SORT(c) sort((c).begin(),(c).end())
const double PI = acos(-1.0);
const double EPS = 1e-9;
const ll BIG_PRIME7 = 1000000007;
const ll BIG_PRIME9 = 1000000009;

template <typename T>
T gcd(T x, T y) {return y ? gcd(y, x%y): x;}

vector<ll> vi,vv;
vector<string> vs;
pair<ll,ll> pii;
vector<pair <ll, ll> >vp;
set<ll> seti;
map<string,int> mymap;
queue<string> que;

char st[11];
ll  ar[100111];
int arr[200][200];
string s,ss,s1,s2,ss1,ss2,jami;
ll q,i,j,k,m,n,h,d,g,t,l,w,r,a,b,c,sum;
ll I,J,L,K,M,N,C,R,SUM,maxi,mini;
bool ind, flag;
double x,y,z,ans;
unsigned long long UI,UJ;

ll f(int l, int r){
	int mini = l;
	for(int i=l;i<=r;i++)
		if(ar[mini]>ar[i])
			mini = i;
	return ar[l] - ar[mini];
}

int main(){
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>ar[i];
	int start = 0;
	int end;
	while(true){
		end = start;
		while( end<n-1 && ar[end]>ar[end+1]){
			end++;
		}
		sum+= f(start,end);
		start = end+1;
		if(start>=n) break;
	}
	cout<<sum<<endl;
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define f0(i,n) for((int)i=0;(int)i<(int)n;i++)
#define SORT(c) sort((c).begin(),(c).end())
const double PI = acos(-1.0);
const double EPS = 1e-9;
const ll BIG_PRIME7 = 1000000007;
const ll BIG_PRIME9 = 1000000009;

template <typename T>
T gcd(T x, T y) {return y ? gcd(y, x%y): x;}

vector<ll> vi,vv;
vector<string> vs;
pair<ll,ll> pii;
vector<pair <ll, ll> >vp;
set<ll> seti;
map<string,int> mymap;
queue<string> que;

char st[11];
ll  ar[100111];
int arr[200][200];
string s,ss,s1,s2,ss1,ss2,jami;
int q,i,j,k,m,n,h,d,g,t,l,w,r,a,b,c,sum;
ll I,J,L,K,M,N,C,R,SUM,maxi,mini;
bool ind, flag;
double x,y,z,ans;
unsigned long long UI,UJ;

ll f(int l, int r){
	int mini = l;
	for(int i=l;i<=r;i++)
		if(ar[mini]>ar[i])
			mini = i;
	return ar[l] - ar[mini];
}

int main(){
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>ar[i];
	int start = 0;
	int end;
	while(true){
		end = start;
		while( end<n-1 && ar[end]>ar[end+1]){
			end++;
		}
		maxi = max(maxi, f(start,end));
		start = end+1;
		if(start>=n) break;
	}
	cout<<maxi<<endl;
	return 0;
}


#include <iostream>
using namespace std;

void main()
{
	long long f,n,a,b=-1,s=0;
	cin >> n;
	cin >> b;
	for(f=1;f<n;f++)
	{
		cin >> a;
		if(a<b)
			s+=b-a;
		b=a;
	}
	cout<<s;
}