#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
long long int powmod(long long int a, long long int b) {
  long long int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int nxt() {
  int x;
  scanf("%d", &x);
  return x;
}
int main() {
  int x, y;
  cin >> x;
  int z = 0;
  int stool = 0;
  vector<int> thelist;
  for (int i = 0; i < x; i++) {
    cin >> y;
    thelist.push_back(y);
  }
  for (auto it = thelist.begin(); it != thelist.end(); it++) {
    if (*it >= z) {
      z = *it;
    } else {
      stool += z - *it;
    }
  }
  cout << stool;
}
