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

int main() {
    int n;
    cin >> n;
    if (n <= 0) {
        cout << 0 << '\n';
        return 0;
    }

    long long buy1 = LLONG_MIN / 4, sell1 = 0;
    long long buy2 = LLONG_MIN / 4, sell2 = 0;

    for (int i = 0; i < n; ++i) {
        int p;
        cin >> p;
        buy1  = max(buy1, -1LL * p);
        sell1 = max(sell1, buy1 + p);
        buy2  = max(buy2, sell1 - p);
        sell2 = max(sell2, buy2 + p);
    }

    cout << sell2 << '\n';
    return 0;
}