#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const int mod = 1e9 + 7;
const int N = 1010;
unsigned long long p_hash(unsigned long long x, unsigned long long y) {
  return (x + y) * (x + y + 1) / 2 + y;
}
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
int h, w, ch, cw, dh, dw;
char s[N][N];
bool mark[N][N];
int warp[N][N];
void dfs(int x, int y) {
  if (x == dh && y == dw) return;
  int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
  bool flag = false;
  for (int i = (0); i < (4); i++) {
    int xx = x + dx[i], yy = y + dy[i];
    if (xx >= 1 && xx <= h && yy >= 1 && yy <= w && s[xx][yy] == '.') {
      if (!mark[xx][yy]) {
        flag = true;
        mark[xx][yy] = true;
        warp[xx][yy] = warp[x][y];
        dfs(xx, yy);
      } else {
        if (warp[x][y] < warp[xx][yy]) {
          warp[xx][yy] = warp[x][y];
          dfs(xx, yy);
        }
      }
    }
  }
  int wx[] = {1, -1, 1, -1}, wy[] = {-1, 1, 1, -1};
  for (int i = (0); i < (3); i++) {
    for (int j = (0); j < (3); j++) {
      for (int k = (0); k < (4); k++) {
        int xx = x + wx[i] * i, yy = y + wy[i] * j;
        if (xx >= 1 && xx <= h && yy >= 1 && yy <= w && s[xx][yy] == '.') {
          if (!mark[xx][yy]) {
            flag = true;
            mark[xx][yy] = true;
            warp[xx][yy] = warp[x][y] + 1;
            dfs(xx, yy);
          } else {
            if (warp[x][y] + 1 < warp[xx][yy]) {
              warp[xx][yy] = warp[x][y] + 1;
              dfs(xx, yy);
            }
          }
        }
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  int 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] < a[i - 1]) {
      int t = a[i - 1] - a[i];
      a[i] += t;
      ans += t;
    }
  }
  cout << ans << endl;
  return 0;
}
