#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
struct Nod {
  i64 mx, lazy;
};
const int N = 1e6 + 5;
Nod pom[N * 4];
i64 dp[N];
int v[N];
vector<int> stk_mn, stk_mx;
int n, ql, qr;
i64 uval;
static void prop(int nod) {
  pom[nod].mx += pom[nod].lazy;
  pom[2 * nod].lazy += pom[nod].lazy;
  pom[2 * nod + 1].lazy += pom[nod].lazy;
  pom[nod].lazy = 0;
}
static i64 eval(int nod) { return pom[nod].mx + pom[nod].lazy; }
static void update(int nod, int st, int dr) {
  if (ql <= st && dr <= qr) {
    pom[nod].lazy += uval;
    return;
  }
  int mid = (st + dr) / 2;
  prop(nod);
  if (ql <= mid) update(2 * nod, st, mid);
  if (mid < qr) update(2 * nod + 1, mid + 1, dr);
  pom[nod].mx = max(eval(2 * nod), eval(2 * nod + 1));
}
static i64 query(int nod, int st, int dr) {
  if (ql <= st && dr <= qr) return eval(nod);
  int mid = (st + dr) / 2;
  i64 ant = 0;
  prop(nod);
  if (ql <= mid) ant = max(ant, query(2 * nod, st, mid));
  if (mid < qr) ant = max(ant, query(2 * nod + 1, mid + 1, dr));
  return ant;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> v[i];
  stk_mn.push_back(1);
  stk_mx.push_back(1);
  for (int t, i = 2; i <= n; ++i) {
    qr = i - 2;
    while (!stk_mn.empty() && v[stk_mn.back()] >= v[i]) {
      t = stk_mn.back();
      stk_mn.pop_back();
      ql = stk_mn.empty() ? 0 : stk_mn.back();
      uval = v[t];
      update(1, 0, n);
      qr = ql - 1;
    }
    qr = i - 1;
    ql = stk_mn.empty() ? 0 : stk_mn.back();
    uval = -v[i];
    update(1, 0, n);
    qr = i - 2;
    while (!stk_mx.empty() && v[stk_mx.back()] <= v[i]) {
      t = stk_mx.back();
      stk_mx.pop_back();
      ql = stk_mx.empty() ? 0 : stk_mx.back();
      uval = -v[t];
      update(1, 0, n);
      qr = ql - 1;
    }
    qr = i - 1;
    ql = stk_mx.empty() ? 0 : stk_mx.back();
    uval = v[i];
    update(1, 0, n);
    stk_mx.push_back(i);
    stk_mn.push_back(i);
    ql = 0, qr = i - 1;
    dp[i] = query(1, 0, n);
    ql = qr = i;
    uval = dp[i];
    update(1, 0, n);
  }
  cout << dp[n] << endl;
  return 0;
}
