#include <bits/stdc++.h>
using namespace std;
long long BigMod(long long base, long long pow, long long mod) {
  if (pow == 0 || base == 1)
    return 1;
  else if (pow == 1)
    return base;
  else if (pow % 2)
    return BigMod(base, pow - 1, mod) * base % mod;
  else {
    long long x = (BigMod(base, pow / 2, mod) % mod);
    x = (x * x) % mod;
    return x;
  }
}
long long isprime(long long prime) {
  long long cnt = 0;
  for (int i = 2; i * i <= prime; i++) {
    if (prime % i == 0) return false;
  }
  return true;
}
void DBG() { cerr << "]" << '\n'; }
template <class H, class... T>
void DBG(H h, T... t) {
  cerr << h;
  if (sizeof...(t)) cerr << ", ";
  DBG(t...);
}
const int dx[] = {+1, -1, +0, +0};
const int dy[] = {+0, +0, +1, -1};
const int f1x[] = {+0, +0, +1, -1, -1, +1, -1, +1};
const int f1y[] = {-1, +1, +0, +0, +1, +1, -1, -1};
const int f2x[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int f2y[] = {-1, 1, -2, 2, -2, 2, -1, 1};
const int maX = 1e3 + 10;
long long vis[maX][maX];
long long n, m;
void DFS(long long x, long long y) {
  cout << x << ' ' << y << '\n';
  vis[x][y] = 1;
  for (long long i = 0; i < 4; i++) {
    long long d = x + dx[i];
    long long dd = y + dy[i];
    if (d >= 1 && d <= n && dd >= 1 && dd <= m && vis[d][dd] != 1) DFS(d, dd);
  }
  for (long long i = x; i <= n; i++)
    if (vis[i][y] == 0) DFS(i, y);
  for (long long i = x - 1; i >= 1; i--)
    if (vis[i][y] == 0) DFS(i, y);
  for (long long i = y; i <= m; i++)
    if (vis[x][i] == 0) DFS(x, i);
  for (long long i = y - 1; i >= 1; i--)
    if (vis[x][i] == 0) DFS(x, i);
}
void solve() {
  long long n, i, j, cc = 0, l, r;
  cin >> l >> r;
  if (l == r) {
    cout << 0 << '\n';
    return;
  }
  j = l ^ r;
  while (j) {
    cc++;
    j >>= 1;
  }
  long long ans = 0;
  while (cc) {
    ans <<= 1;
    ans++;
    cc--;
  }
  cout << ans << '\n';
}
int main() {
  long long n, m, i, j, k, len = 0, e, p, q, a, b, c, d;
  string str;
  int t;
  long long cnt = 0;
  cin >> n;
  vector<long long> v(n);
  for (auto &el : v) cin >> el;
  for (i = 0; i < n; i++) {
    if (v[i] > v[i + 1]) {
      cnt += (v[i] - v[i + 1]);
      v[i + 1] = v[i];
    }
  }
  cout << cnt << '\n';
  return 0;
}
