#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int mpow(int base, int exp);
long long randGen(long long lower, long long upper) {
  return (rand() % (upper - lower + 1)) + lower;
}
long long dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  int mx = a[0], ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] > mx) mx = a[i];
    ans += mx - a[i];
  }
  cout << ans << endl;
}
int main() {
  int t = 1;
  while (t-- > 0) {
    solve();
  }
  return 0;
}
int mpow(int base, int exp) {
  base %= MOD;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((long long)result * base) % MOD;
    base = ((long long)base * base) % MOD;
    exp >>= 1;
  }
  return result;
}
