#include <bits/stdc++.h>
const long long N = 3 * 1e5 + 5;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long inf = 1e18;
using namespace std;
bool ok;
void dfs1(long long u, bool vis[], long long tot[], long long p[],
          long long h[], std::vector<long long> v[]) {
  if (vis[u] == 1) return;
  vis[u] = 1;
  long long sum = 0;
  sum += p[u];
  long long sorr = 0;
  for (long long i = 1; i < v[u].size(); i++) {
    dfs1(v[u][i], vis, tot, p, h, v);
    sum += tot[v[u][i]];
    sorr = sorr + tot[v[u][i]] - h[v[u][i]];
  }
  tot[u] = sum;
  return;
}
void dfs2(long long u, bool vis[], long long tot[], long long p[],
          long long h[], std::vector<long long> v[]) {
  if (vis[u] == 1) return;
  vis[u] = 1;
  long long sorr = 0;
  for (long long i = 1; i < v[u].size(); i++) {
    dfs2(v[u][i], vis, tot, p, h, v);
    sorr = sorr + tot[v[u][i]] - h[v[u][i]];
  }
  if (tot[u] - h[u] > sorr + 2 * p[u]) {
    ok = false;
  }
  if ((tot[u] - h[u]) % 2 == 1) {
    ok = false;
  }
  if (h[u] > (tot[u]) or h[u] < (-tot[u])) {
    ok = false;
  }
  return;
}
int dfs(int u, vector<vector<long long> > &v, bool vis[],
        vector<long long> &ans, int n) {
  if (vis[u] == 1) return 0;
  vis[u] = 1;
  int num = 0;
  for (int i = 0; i < v[u].size(); i++) {
    int y = dfs(v[u][i], v, vis, ans, n);
    if (y) ans.push_back(y);
    num += y;
  }
  num++;
  return num;
}
void solve() {
  int n;
  cin >> n;
  int a[n];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      ans += (a[i - 1] - a[i]);
      a[i] = a[i - 1];
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  long long t = 1;
  long long ctr = 0;
  while (t--) {
    ctr++;
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
}
