#include <bits/stdc++.h>
static const long long MX = 100005;
static const long long MX_ll = 1e18;
const double PI = acos(-1);
using namespace std;
int changeDoubleToInt(string decimal) {
  decimal.erase(1, 1);
  int n_decimal = atoi(decimal.c_str());
  return n_decimal;
}
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int IsPrime_tf(long long num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;
  for (long long i = 3; i * i <= num; i += 2) {
    if (num % i == 0) return false;
  }
  return true;
}
long long IsPrime_value(long long num) {
  if (num < 2)
    return -1;
  else if (num == 2)
    return 2;
  for (long long i = 2; i * i <= num; i += 1) {
    if (num % i == 0) return i;
  }
  return num;
}
int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < (n); i++) cin >> a[i];
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i - 1] > a[i]) {
      ans += a[i - 1] - a[i];
      a[i] += a[i - 1] - a[i];
    }
  }
  cout << ans << endl;
}
