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

int N;
int* arr;
int ans;
void input (){
    do{
        cin >> N;
    }while(N < 2);
    arr = (int*)malloc(N * sizeof(int));
    for(int i = 0; i < N; i++){
        cin >> arr[i];
    }
}

int getSecondMax(int n, int arr1[]){
    int firstMax = arr1[0];
    int previousFirstMax = arr1[0];
    int secondMax = INT_MIN;
    for(int i = 0; i < n; i++){
        if(arr1[i] >= firstMax){
            previousFirstMax = firstMax;
            firstMax = arr1[i];
        }
        else if(arr1[i] > secondMax && arr1[i] >= previousFirstMax && previousFirstMax != firstMax){
            secondMax = arr1[i];
        }
        else if(secondMax < previousFirstMax && previousFirstMax != firstMax){
            secondMax = previousFirstMax;
        }
        else if(arr1[i] > secondMax){
            secondMax = arr1[i];
        }
    }
    if(previousFirstMax != firstMax && previousFirstMax > secondMax){
        secondMax = previousFirstMax;
    }
    return secondMax;
}

void best_subarray(){
    int currentMax = INT_MIN;
    for(int i = 0; i < N - 1; i++){
        for(int j = i + 1; j < N; j++){
            int len = j - i + 1;
            if(len < 2){
                continue;
            }
            int newArr[len];
            for(int k = i, l = 0 ; k <= j; k++, l++){
                newArr[l] = arr[k];
            }
            int secondMax  = getSecondMax(len, newArr) - len;
            if(secondMax > currentMax){
                currentMax = secondMax;
            }

        }
    }
    ans = currentMax;
}

void output(){
    cout << ans << endl;
}

int main()
{
    input();
    best_subarray();
    output();
    return 0;
}
