#include <iostream>
#include <algorithm>
 
using namespace std;
int main(void){
    int A[100001];
    int tani[50001], ts[50001];
    int yama[50001], ys[50001];
    int jjj[100001][4];
    int poy[50001][2];
    int pot[50001][2];
    int N, i, l, j=0, t=0, w = 0, f = 0, lt, ly, llt, lly, a;
    char m = 'L';
    int hw = 0, lh = 1000000000,wl,wh,vmax;

    cin >> N;
    for(l=0; l < N; l++) {
        cin >> i;
        if (i < t) {
            if(m == 'H') {
                A[j++] = t;
                m = 'L';
                t = i;
            } else t = i;
        } else if(i > t) {
            if(m == 'L') {
                A[j++] = t;
                m = 'H';
                t = i;
            } else t = i;
        } else {    ;   }
    }
    if(m == 'H') {
        A[j++] = i;     // last one write
        A[j++] = 0;     // last sea write
    } else {
        A[j++] = 0;     // last sea write
    }
    for(i = 0, lt=0;i < j; i += 2,lt++){
        if(hw < A[i]) hw = A[i]; 
        ts[lt] = tani[lt] = A[i];
//        cout << "ts : " << ts[lt] << endl; 
    }
    for(i = 1, ly=0;i < j; i += 2,ly++){
        if(lh > A[i]) lh = A[i];
        ys[ly] = yama[ly] = A[i]; 
//        cout << "ys : " << ys[ly] << endl; 
    }
    A[j++] = 0;
    sort(ys, ys+ly);
    sort(ts, ts+lt);
// uniq
    lly = ly - 1, llt = lt -1;
    wl = i = 0;
    poy[i][1] = 1; poy[i][0] = wl = ys[lly--]; 
    while(lly >= 0) {
        if (wl == ys[lly]) {
            poy[i][1]++;
            lly--;
//            cout << "poy : " << poy[i][1] << endl; 
        } else {
            i++;
            poy[i][1] = 1;
            poy[i][0] = wl = ys[lly--];
        }
    }
    i++;
    poy[i][0] = poy[i][1] = 0;
    lly = i;
    wl = i = 0;
    pot[i][1] = 1; pot[i][0] = wl = ts[llt--];
    while(llt >= 0) {
        if (wl == ts[llt]) {
            pot[i][1]++;
            llt--;
//            cout << "pot : " << pot[i][1] << endl; 
        } else {
            i++;
            pot[i][1] = 1;
            pot[i][0] = wl = ts[llt--];
        }
    }
    i++;
    pot[i][0] = pot[i][1] = 0;
    llt = i;
//marge + count
    i = l = a = 0;
    int sums = 0;
    while(poy[i][0] != 0) {
        if(poy[i][0] == pot[l][0]) {
            jjj[a][0] = poy[i][0];
            jjj[a][1] = poy[i++][1];
            jjj[a][2] = pot[l++][1];
        } else if(poy[i][0] < pot[l][0]) {
            jjj[a][0] = pot[l][0];
            jjj[a][1] = 0;
            jjj[a][2] = pot[l++][1];
        } else if(poy[i][0] > pot[l][0]) {
            jjj[a][0] = poy[i][0];
            jjj[a][1] = poy[i++][1];
            jjj[a][2] = 0;
        }
        jjj[a][3] = sums += (jjj[a][1] - jjj[a][2]);
//        cout << jjj[a][0] << " " << jjj[a][3] << " " << jjj[a][1] << " " << jjj[a][2] <<endl;
        a++;
    }
    vmax = 0;
    while(a > 0) {
        a--;
        if (vmax < jjj[a][3]) vmax = jjj[a][3];
//        cout << jjj[a][0] << " " << jjj[a][3] << " " << jjj[a][1] << " " << jjj[a][2] <<endl;
    }
    cout << vmax << endl;

/*
    wh = max(lh, hw) + 1;
    w = wl = min(lh, hw) -1;
    while(1) {
        int e = 0, c = j, s = 1000000000;
        while(c > 0) {
            c--;
            if(A[c] > w) {
                if(A[c] < s) s = A[c];
                if(A[c+1] <= w) e++;
            }
        }
        if(e > f) f = e;
        if(w > wh) break;

        w = s;
    }
    cout << f << endl;
*/
    return 0;
}




Measured memory_bytes: 5244