#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); i++) {
    cin >> a[i];
  }
  vector<int> b(n);
  b[0] = 0;
  int ans = 0;
  for (int i = 0; i < (n); i++) {
    if (i == 0) continue;
    if (a[i] < a[i - 1]) {
      b[i] = abs(a[i] - a[i - 1]);
      ans += b[i];
      a[i] = a[i] + b[i];
    }
  }
  cout << ans;
  return 0;
}
