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

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

void bubbleSort(int n, int arr1[]){
    for(int i = 0; i < 2; i++){
        for(int j = i + 1; j < n; j++){
            if(arr1[i] < arr1[j]){
                int tmp = arr1[i];
                arr1[i] = arr1[j];
                arr1[j] = tmp;
            }
        }
    }
}

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];
            }
            bubbleSort(len, newArr);
            if(newArr[1] - len > currentMax){
                currentMax = newArr[1] - len;
            }

        }
    }
    ans = currentMax;
}

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

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