#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> to[100010];
bool visited[100010];
long long dist[100010];
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
void dfs(int now, long long d) {
  visited[now] = true;
  dist[now] = d;
  for (auto p : to[now]) {
    if (visited[p.first]) continue;
    dfs(p.first, d + p.second);
  }
}
vector<int> ans(10005, 0);
void f(long long n) {
  for (long long x = 1; x * x < n; x++) {
    for (long long y = 1; y * y < n; y++) {
      for (long long z = 1; z * z < n; z++) {
        long long v = (x * x) + (y * y) + (z * z) + (x * y) + (y * z) + (z * x);
        if (v < 10005) ans[v]++;
      }
    }
  }
}
int find(long long k) {
  long long tag = 7777777;
  int cnt = 0;
  cout << tag % k << endl;
  while (tag % k) {
    cnt++;
    tag /= 10;
    cout << tag << endl;
  }
  return cnt;
}
int main() {
  long long n;
  cin >> n;
  vector<long long> p(n);
  for (int i = 0; i < (n); ++i) {
    cin >> p[i];
  }
  long long ans = 0;
  for (int i = 0; i < (n); ++i) {
    if (p[i] > p[i + 1]) {
      ans += p[i] - p[i + 1];
      p[i + 1] = p[i];
    }
  }
  cout << ans << endl;
  return 0;
}
