#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
void very_fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long binary_exponentation(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = (ret * a) % mod;
    b >>= 1;
    a = (a * a) % mod;
  }
  return ret;
}
int32_t main() {
  very_fast();
  {
    long long n;
    cin >> n;
    long long a[n];
    for (long long i = 0; i < n; i++) cin >> a[i];
    long long maxi = a[0], sum = 0;
    for (long long i = 1; i < n; i++) {
      if (a[i] > maxi) {
        maxi = a[i];
        sum += abs(a[i] - a[i - 1]);
      }
    }
    cout << sum << endl;
  }
  return 0;
}
