#include <bits/stdc++.h>
using namespace std;
int int_pow(int x, int p) {
  if (p == 0) return 1;
  if (p == 1) return x;
  int tmp = int_pow(x, p / 2);
  if (p % 2 == 0)
    return tmp * tmp;
  else
    return x * tmp * tmp;
}
void fill(istream& in, vector<int>& a, int n) {
  int elm;
  for (int i = 0; i < n; ++i) {
    in >> elm;
    a.push_back(elm);
  }
}
void fill(istream& in, vector<long long>& a, int n) {
  long long elm;
  for (int i = 0; i < n; ++i) {
    in >> elm;
    a.push_back(elm);
  }
}
void fill(vector<int>& a, int n, int val) {
  for (int i = 0; i < n; ++i) {
    a.push_back(val);
  }
}
void fill(vector<long long>& a, int n, long long val) {
  for (int i = 0; i < n; ++i) {
    a.push_back(val);
  }
}
int sum(vector<int> arr) {
  int s = 0;
  for (auto& x : arr) s += x;
  return s;
}
int max(vector<int> a) { return *max_element(a.begin(), a.end()); }
int min(vector<int> a) { return *min_element(a.begin(), a.end()); }
void print(ostream& out, vector<int> a) {
  for (auto& x : a) {
    out << x << " ";
  }
  out << endl;
}
void print(ostream& out, vector<long long> a) {
  for (auto& x : a) {
    out << x << " ";
  }
  out << endl;
}
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
long long power(unsigned long long a, unsigned long long b,
                unsigned long long ret = 1LL) {
  while (b--) ret = (ret * a);
  return ret;
}
#pragma GCC optimize("Ofast")
static auto speedup = []() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  return 0;
}();
class CStep {
 public:
  void solve(std::istream& in, std::ostream& out) {
    int n;
    in >> n;
    vector<int> as;
    fill(in, as, n);
    int ans = 0;
    int prev = as[0];
    for (int i = 1; i < n; ++i) {
      if (as[i] < prev) {
        ans += prev - as[i];
      } else {
        prev = as[i];
      }
    }
    out << ans << endl;
  }
};
int main() {
  CStep solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
