#include <bits/stdc++.h>
using namespace std;
bool comp(int x, int y) { return x < y; }
int to_int(char c) { return c; }
long long getMin(long long arr[], long long n) {
  long long res = 1e6;
  for (int i = 0; i <= n; i++) {
    res = min(res, arr[i]);
  }
  return res;
}
string divisors(long long n) {
  long long cnt = 0;
  for (long long i = n; i >= 1; i--) {
    if (n % i == 0) {
      cnt++;
    }
    if (cnt > 3) {
      return "NO";
    }
  }
  if (cnt == 3) {
    return "YES";
  }
  return "NO";
}
bool isPrime(long long n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}
void dispPrime(long long n) {
  long long num = 1e6;
  bool prime[num + num + num];
  memset(prime, true, sizeof(prime));
  for (int i = 2; i * i < num + num + num; i++) {
    if (prime[i]) {
      for (int j = i * 2; j < num + num + num; j += i) {
        prime[j] = false;
      }
    }
  }
  long long k = 2;
  long long cnt = 0;
  long long upto = n;
  while (true) {
    if (k < num + num + num) {
      if (prime[k]) {
        cnt++;
        if ((k * k) == n) {
          cout << "YES"
               << "\n";
          return;
        }
      }
    }
    if (k >= upto) {
      cout << "NO"
           << "\n";
      break;
    }
    k++;
  }
}
int mltply(int n, int f[], int sz) {
  int carry = 0;
  for (int i = 0; i < sz; i++) {
    int prod = f[i] * n + carry;
    f[i] = prod % 10;
    carry = prod / 10;
  }
  while (carry) {
    f[sz] = carry % 10;
    carry /= 10;
    sz++;
  }
  return sz;
}
void factorial(int n) {
  int f[500];
  f[0] = 1;
  int sz = 1;
  for (int i = 2; i < n + 1; i++) {
    sz = mltply(i, f, sz);
  }
  int ans = 0;
  for (int i = sz - 1; i >= 0; i--) {
    ans += f[i];
  }
  cout << ans << "\n";
}
int main() {
  long long n;
  cin >> n;
  long long arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  long long mx = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    if (mx < arr[i]) {
      mx = arr[i];
    }
    if (i > 0) {
      if (arr[i] < arr[i - 1]) {
        ans += mx - arr[i];
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
