#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005, logn = 20;
int n, a[maxn], mst[maxn][logn], mast[maxn][logn], gg[maxn];
deque<int> hull;
long long dp[maxn];
inline int qmin(int i, int j) {
  int g = gg[j - i + 1];
  return min(mst[i][g], mst[j - (1 << g) + 1][g]);
}
inline int qmax(int i, int j) {
  int g = gg[j - i + 1];
  return max(mast[i][g], mast[j - (1 << g) + 1][g]);
}
inline long long f(int i, int j) {
  if (i > j) return 0;
  return qmax(i, j) - qmin(i, j);
}
inline int inter(int a, int b) {
  int lo = b + 1, hi = n, ans = n + 1;
  while (lo <= hi) {
    int m = (lo + hi) >> 1;
    if (dp[a] + f(a + 1, m) < dp[b] + f(b + 1, m)) {
      ans = m;
      hi = m - 1;
    } else
      lo = m + 1;
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf(" %d", &a[i]);
    mst[i][0] = mast[i][0] = a[i];
    gg[i] = log2(i);
  }
  for (int s = 1; (1 << s) <= n; s++) {
    for (int l = 1; l + (1 << (s - 1)) <= n; l++) {
      mst[l][s] = min(mst[l][s - 1], mst[l + (1 << (s - 1))][s - 1]);
      mast[l][s] = max(mast[l][s - 1], mast[l + (1 << (s - 1))][s - 1]);
    }
  }
  hull.push_back(0);
  for (int i = 1; i <= n; i++) {
    while (hull.size() >= 2 && inter(hull[0], hull[1]) <= i) hull.pop_front();
    dp[i] = dp[hull[0]] + f(hull[0] + 1, i);
    while (hull.size() >= 2 &&
           inter(hull[hull.size() - 2], hull.back()) >= inter(hull.back(), i))
      hull.pop_back();
    hull.push_back(i);
  }
  printf("%lld\n", dp[n]);
}
