#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MOD = 1e9 + 7;
constexpr long double pi = 3.141592653589793238462643383279;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int fact(int i) {
  if (i == 0) return 1;
  return ((fact(i - 1)) * i) % MOD;
}
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
int keta(int n) {
  if (n == 0) return 1;
  int count = 0;
  while (n != 0) {
    n /= 10;
    count++;
  }
  return count;
}
int ketasum(int n) {
  int sum = 0;
  while (n != 0) {
    sum += n % 10;
    n /= 10;
  }
  return sum;
}
ll modpow(ll a, ll n, ll mod) {
  ll hi = 1;
  while (n > 0) {
    if (n & 1) hi = hi * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return hi;
}
char toSmall(char c) { return (c + 0x20); }
char toLarge(char c) { return (c - 0x20); }
float myPower(int a, int n) {
  float x = 1;
  while (n > 0) {
    if (n & 1) {
      x *= a;
    }
    a *= a;
    n >>= 1;
  }
  return x;
}
bool is_prime(const unsigned n) {
  switch (n) {
    case 0:
    case 1:
      return false;
    case 2:
    case 3:
      return true;
  }
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (unsigned i = 5; i * i <= n; i += 6) {
    if (n % i == 0) return false;
    if (n % (i + 2) == 0) return false;
  }
  return true;
}
int ans;
int main(void) {
  int n;
  cin >> n;
  vector<int> vec(n);
  for (int i = 0; i < n; i++) cin >> vec[i];
  for (int i = 0; i < n; i++) {
    int a = max(0, vec[i + 1] - vec[i]);
    vec[i] += a;
    ans += a;
  }
  cout << ans << "\n";
  return 0;
}
