#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, res = 0;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      if (a[i] > a[i + 1]) res += abs(a[i] - a[i + 1]);
    } else if (i > 0 and i < n - 1) {
      if (a[i] > a[i - 1] and a[i] < a[i + 1])
        res += abs(a[i] - a[i - 1]);
      else if (a[i] < a[i - 1] and a[i] > a[i + 1])
        res += abs(a[i] - a[i + 1]);
    } else if (i == n - 1) {
      res += *max_element(a.begin(), a.end()) - a.back();
    }
  }
  cout << res;
  return 0;
}
