#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 1;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int n;
int main() {
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  int res = 0, inf = a[0];
  for (int i = 0; i < n; i++) {
    if (a[i] <= inf) {
      res += inf - a[i];
    } else if (a[i] > inf) {
      inf = a[i];
    }
  }
  cout << res << endl;
  return 0;
}
